首先 创建表名:
CREATE TABLE [dbo].[tblEmployee]
(
[EmpId] [nchar](50) NULL,
[EmpName] [nchar](50) NULL,
[EmpEmail] [nchar](50) NULL
) ON [PRIMARY]
其次 创建webService
public class Service : WebService {
SqlConnection con = new SqlConnection ("Data Source=localhost;Initial Catalog=test;User id=;Password="); // Method to get all records from database
[WebMethod]
public List<Employee> GetEmployees()
{
// Method does not follow any coding standards. This is just for demo purposes.
var emplist = new List<Employee>();
Employee emp;
var da = new SqlDataAdapter("SELECT EmpId, EmpName, EmpEmail from tblEmployee", con);
var dt = new DataTable();
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
emp = new Employee {
EmpId = dr["EmpId"].ToString(),
EmpName = dr["EmpName"].ToString(),
EmpEmail = dr["EmpEmail"].ToString() };
emplist.Add(emp);
}
return emplist;
}
// Method to save a record
[WebMethod]
public void SaveEmployee(string empId, string empName, string empEmail)
{
// Method does not follow any coding standards. This is just for demo purposes.
var cmd = new SqlCommand("INSERT INTO tblEmployee (EmpId, EmpName, EmpEmail) VALUES(@empid, @empname, @empemail)",con);
cmd.Parameters.Add(new SqlParameter("@empid", empId));
cmd.Parameters.Add(new SqlParameter("@empname", empName));
cmd.Parameters.Add(new SqlParameter("@empemail", empEmail));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
// Employee Class public class Employee {
public string EmpId = string.Empty;
public string EmpName = string.Empty;
public string EmpEmail = string.Empty;
}
最后 创建MXML文件
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"> <mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private function init():void {
// Get Data from WebService and fill datagrid when you fist invoke the application
ws.GetEmployees();
}
private function GetEmployees(event:ResultEvent):void {
// Databind data from webservice to datagrid
datagrid.dataProvider = event.result;
}
private function SaveEmployee(event:ResultEvent):void {
// To Refresh DataGrid;
ws.GetEmployees();
Alert.show("Saved Successfully");
}
private function AddRecord(event:MouseEvent):void {
// Save a record using a WebService method
ws.SaveEmployee(txtEmpId.text, txtEmpName.text, txtEmpEmail.text);
//
}
private function fault(event:FaultEvent):void {
// Oppps some error occured
Alert.show(event.toString());
}
]]>
</mx:Script>
<!-- WebService definition -->
<mx:WebService id="ws" wsdl="http://localhost/TestWebService/Service.asmx?WSDL" fault="fault(event)"> <mx:operation name="GetEmployees" resultFormat="object" result="GetEmployees(event)" /> <mx:operation name="SaveEmployee" resultFormat="object" result="SaveEmployee(event)" /> </mx:WebService>
<mx:Panel x="41.5" y="66" width="714.5" height="237" layout="absolute" title="ASP.NET WebService + Flex Demonstration">
<mx:HBox height="95%" width="95%" horizontalCenter="0" verticalCenter="0">
<mx:DataGrid id="datagrid" width="465" height="100%">
<mx:columns>
<mx:DataGridColumn headerText="Emp Id" dataField="EmpId"/>
<mx:DataGridColumn headerText="Emp Name" dataField="EmpName"/>
<mx:DataGridColumn headerText="Emp Email" dataField="EmpEmail"/>
</mx:columns>
</mx:DataGrid>
<mx:Form x="608" y="74" width="100%" height="100%" borderStyle="solid">
<mx:FormItem label="EmpId">
<mx:TextInput width="106" id="txtEmpId"/>
</mx:FormItem>
<mx:FormItem label="EmpName">
<mx:TextInput width="106" id="txtEmpName"/>
</mx:FormItem>
<mx:FormItem label="EmpEmail">
<mx:TextInput width="106" id="txtEmpEmail"/>
</mx:FormItem>
<mx:FormItem width="156" horizontalAlign="right">
<mx:Button label="Add" id="btnAdd" click="AddRecord(event)"/>
</mx:FormItem>
</mx:Form>
</mx:HBox>
</mx:Panel>
</mx:Application>
然后你就成功了