Flex4.6 + WebService(C#) + SQLServer2005+DataGrid增删查改程序
注意:
本实例供大家参考!程序中添加,删除,修改功能,有时可能没有及时同步到DataGrid控件中,希望大家帮忙找下原因,告诉我一下!谢谢!!!
服务器名称:FEIYING\\SQL2005
数据库:test
用户名:sa;
密 码:by3g
如果你还不知道怎样建立WebServices程序请参考:http://blog.csdn.net/wuxiaokaixinguo/article/details/8117398
第一部分:建库,建表
第一步:在SQLServer2005中建立test数据
第二步:在test数据库下建立T_Users表,字段为:userName varchar(20); userPass varchar(20); 并且设置userName为主建
第三步:在表中添加数据
insert into Test.dbo.T_Users values('a','a');
insert into Test.dbo.T_Users values('b','b');
insert into Test.dbo.T_Users values('c','c');
insert into Test.dbo.T_Users values('d','d');
insert into Test.dbo.T_Users values('e','e');
insert into Test.dbo.T_Users values('f','f');
insert into Test.dbo.T_Users values('g','g');
第二部分:编写WebService程序
在VS2005中建立一个ASP.NET Web服务
代码如下:
using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Data; using System.Data.SqlClient; using System.Xml; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { public Service() { } //添加用户 [WebMethod] public int addUser(string userName,string userPass) { SqlConnection conn = new SqlConnection("Data Source=FEIYING\\SQL2005; Initial Catalog=test; uid=sa; pwd=by3g"); conn.Open(); string strSql = "insert into T_Users(userName,userPass) values(@userName,@userPass)"; SqlCommand strCmd = new SqlCommand(strSql, conn); SqlParameter para_userName = new SqlParameter("@userName",userName); strCmd.Parameters.Add(para_userName); SqlParameter para_userPass = new SqlParameter("@userPass",userPass); strCmd.Parameters.Add(para_userPass); int result = strCmd.ExecuteNonQuery(); conn.Close(); strCmd.Dispose(); return result; } //删除用户 [WebMethod] public int delUser(string userName) { SqlConnection conn = new SqlConnection("Data Source=FEIYING\\SQL2005; Initial Catalog=test; uid=sa; pwd=by3g"); conn.Open(); string strSql = "delete from T_Users where userName=@userName"; SqlCommand strCmd = new SqlCommand(strSql, conn); SqlParameter para_userName = new SqlParameter("@userName", userName); strCmd.Parameters.Add(para_userName); int result = strCmd.ExecuteNonQuery(); conn.Close(); strCmd.Dispose(); return result; } //查询用户 [WebMethod] public XmlDocument GetList() { try {
DataSet ds = selUser(); XmlDocument xml = new XmlDocument(); xml.LoadXml(ds.GetXml()); return xml; } catch (Exception ex) { XmlDocument xml = new XmlDocument(); xml.LoadXml("<Error>" + ex.Message + "</Error>"); return xml; } } [WebMethod] public DataSet selUser() { string strSql = "SELECT * FROM T_Users"; SqlConnection conn = new SqlConnection("Data Source=FEIYING\\SQL2005; Initial Catalog=test; uid=sa; pwd=by3g"); SqlDataAdapter da = new SqlDataAdapter(strSql,conn); DataSet dt = new DataSet(); da.Fill(dt,"T_Users"); return dt; } //修改用户 [WebMethod] public int udUser(string userName, string userPass) { SqlConnection conn = new SqlConnection("Data Source=FEIYING\\SQL2005; Initial Catalog=test; uid=sa; pwd=by3g"); conn.Open(); string strSql = "update T_Users set userPass=@userPass where userName=@userName"; SqlCommand strCmd = new SqlCommand(strSql, conn); SqlParameter para_userName = new SqlParameter("@userName", userName); strCmd.Parameters.Add(para_userName); SqlParameter para_userPass = new SqlParameter("@userPass", userPass); strCmd.Parameters.Add(para_userPass); int result = strCmd.ExecuteNonQuery(); conn.Close(); strCmd.Dispose(); return result; } } |
第三部分:编写Flex程序
Flex中的代码WebService.mxml
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="btn_selectData()"> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.CloseEvent; [Bindable] public var getResultXML:XML = new XML(); //定义一个变量用来接收数据 public var isSucceed:Boolean = new Boolean(false); //定义一个变量用来判断是否登录成功 <!--重置--> private function btn_resetClickHandle():void{ text_userName.text = ""; text_userPass.text = ""; } <!--添加数据库--> public function btn_addClickHandle():void{ var userName:String = this.text_userName.text; var userPass:String = this.text_userPass.text; var flag:int = this.WebServiceTest.addUser(userName,userPass); if(flag==0) { Alert.show("添加成功","提示"); }else{ Alert.show("添加失败","提示"); } btn_selectData(); btn_resetClickHandle(); } <!--查询数据--> public function btn_selectData():void{ this.WebServiceTest.GetList.send(); } <!--修改数据--> public function btn_updateData():void{ text_userName.text = dg.selectedItem.userName; text_userPass.text =dg.selectedItem.userPass; } protected function btn_save_clickHandler():void { var userName:String = text_userName.text; var userPass:String = text_userPass.text; var flag:int = this.WebServiceTest.udUser(userName,userPass); btn_selectData(); btn_resetClickHandle(); } <!--删除数据--> public function btn_deleteData():void{ var dlg:Object=Alert.show("确认删除吗?", "提示!", Alert.YES|Alert.NO,null,delfun); } public function delfun(event:CloseEvent):void{ if(event.detail==Alert.YES){ var userName:String = dg.selectedItem.userName; WebServiceTest.delUser(userName); Alert.show("已删除","提示"); } btn_selectData(); }
]]> </fx:Script> <fx:Declarations> <mx:WebService id="WebServiceTest" useProxy="false" wsdl="http://localhost:2116/WebService1/Service.asmx?WSDL" showBusyCursor="true"> <mx:operation name="GetList"/> <mx:operation name="addUser"/> <mx:operation name="udUser"/> <mx:operation name="delUser"/> </mx:WebService> </fx:Declarations> <s:Panel x="26" y="10" width="281" height="314" color="#2D39E0" title="DataGrid控件之增删查改"> <mx:DataGrid id="dg" x="10" y="10" width="251" enabled="true" height="157" dataProvider="{WebServiceTest.GetList.lastResult.NewDataSet.T_Users}"> <mx:columns> <mx:DataGridColumn headerText="帐号" width="50" dataField="userName"/> <mx:DataGridColumn headerText="密码" width="50" dataField="userPass"/> <mx:DataGridColumn headerText="修改" editable="false" > <mx:itemRenderer> <fx:Component> <mx:Button label="修改" width="20" click="outerDocument.btn_updateData()"/> </fx:Component> </mx:itemRenderer> </mx:DataGridColumn> <mx:DataGridColumn headerText="删除" editable="false" > <mx:itemRenderer> <fx:Component> <mx:Button label="删除" width="20" click="outerDocument.btn_deleteData()"/> </fx:Component> </mx:itemRenderer> </mx:DataGridColumn> </mx:columns> </mx:DataGrid> <s:Label x="30" y="175" height="16" text="帐号:"/> <s:Label x="30" y="208" width="36" height="19" text="密码:"/> <s:TextInput id="text_userName" x="84" y="175" width="114"/> <s:TextInput id="text_userPass" x="84" y="205" width="114"/> <!--displayAsPassword="true"--> <!--<s:Button id="btn_login" x="22" y="281" width="59" height="33" label="登录" click="btn_loginClickHandle()"/>--> <s:Button id="btn_reset" x="179" y="238" width="60" height="33" label="重置" click="btn_resetClickHandle()"/> <s:Button id="btn_add" x="21" y="237" width="57" height="33" label="添加" click="btn_addClickHandle()"/> <s:Button id="btn_save" x="102" y="235" width="60" height="36" label="保存" click="btn_save_clickHandler()"/> </s:Panel> </s:Application> |
第四部分:测试程序