silverlight通过WCF连接数据库之增删改查

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Runtime.Serialization;
 5 using System.ServiceModel;
 6 using System.Text;
 7 
 8 namespace HouseShopping.Web.APPData.Login
 9 {
10     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“ILoginAndResister”。
11     [ServiceContract]
12     public interface ILoginAndResister
13     {
14         [OperationContract]
15         void OpenSQL();
16         [OperationContract]
17         void CloseSQL();
18         [OperationContract]
19         string QuerySQL(string UserName, string pwd);
20         [OperationContract]
21         int AddUsers(string Username, string pwd);
22         [OperationContract]
23         int UpdateUsers(string Username, string pwd);
24         [OperationContract]
25         int DeleteUsers(string Username);
26     }
27 }
ILoginAndRegister

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data;
  4 using System.Data.SqlClient;
  5 using System.Linq;
  6 using System.Runtime.Serialization;
  7 using System.ServiceModel;
  8 using System.Text;
  9 
 10 namespace HouseShopping.Web.APPData.Login
 11 {
 12     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“LoginAndResister”。
 13     // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 LoginAndResister.svc 或 LoginAndResister.svc.cs,然后开始调试。
 14     public class LoginAndResister : ILoginAndResister
 15     {
 16         SqlConnection strCon = new SqlConnection(@"server=.\MySql;database=AnJuKe_DB;user id=sa;password=123;");
 17         //server=SQLServer服务器名称;database=数据库名称;uid=用户名;pwd=密码"
 18         public void OpenSQL()
 19         {
 20             strCon.Open();
 21         }
 22 
 23         public void CloseSQL()
 24         {
 25             strCon.Close();
 26         }
 27         public string QuerySQL(string UserName, string pwd)
 28         {
 29             try
 30             {
 31                 OpenSQL();
 32                 string strSql = string.Format("select * from T_Users where Name like '%{0}%' and Password like '%{1}%'", UserName, pwd);
 33                 DataSet ds = new DataSet();
 34                 SqlDataAdapter s = new SqlDataAdapter(strSql, strCon);
 35                 s.Fill(ds);
 36                 return ds.GetXml();
 37             }
 38             catch (Exception ex)
 39             {
 40                 throw ex;
 41             }
 42             finally
 43             {
 44                 CloseSQL();
 45             }
 46         }
 47         public int AddUsers(string Username, string pwd)
 48         {
 49 
 50             try
 51             {
 52                 OpenSQL();
 53                 string strInsert = "insert into T_Users values('" + Username + "','" + pwd + "')";
 54                 SqlCommand cmd = new SqlCommand(strInsert, strCon);
 55                 return cmd.ExecuteNonQuery();
 56             }
 57             catch (Exception ex)
 58             {
 59                 throw ex;
 60             }
 61             finally
 62             {
 63                 CloseSQL();
 64             }
 65 
 66         }
 67         public int UpdateUsers(string Username, string pwd)
 68         {
 69 
 70             try
 71             {
 72                 OpenSQL();
 73                 string strUpdate = "UPDATE T_Users SET Password = '" + pwd + "' WHERE Name = '" + Username + "'";
 74                 SqlCommand cmd = new SqlCommand(strUpdate, strCon);
 75                 return cmd.ExecuteNonQuery();
 76             }
 77             catch (Exception ex)
 78             {
 79                 throw ex;
 80             }
 81             finally
 82             {
 83                 CloseSQL();
 84             }
 85         }
 86         public int DeleteUsers(string Username)
 87         {
 88             try
 89             {
 90                 OpenSQL();
 91                 string strDelete = "delete from T_Users where Name='" + Username + "'";
 92                 SqlCommand cmd = new SqlCommand(strDelete, strCon);
 93                 return cmd.ExecuteNonQuery();
 94             }
 95             catch (Exception ex)
 96             {
 97                 throw ex;
 98             }
 99             finally
100             {
101                 CloseSQL();
102             }
103         }
104     }
105 }
LoginAndRegister
 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Net;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Documents;
 8 using System.Windows.Ink;
 9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Animation;
12 using System.Windows.Shapes;
13 using System.Xml;
14 using HouseShopping.MyLoginService;
15 
16 namespace HouseShopping.MyClasses
17 {
18     public class UsersManageClass
19     {
20       private  LoginAndResisterClient client;
21       private TextBlock tb;
22         private string Username;
23         public void BeginRegister( string UserName,string pwd)
24         {           
25             client = new LoginAndResisterClient();
26             client.AddUsersCompleted += client_AddUsersCompleted;
27             client.AddUsersAsync(UserName, pwd);
28         }
29         void client_AddUsersCompleted(object sender, AddUsersCompletedEventArgs e)
30         {
31             if (Convert.ToInt32(e.Result) > 0)
32             {
33                 MessageBox.Show("注册成功!恭喜你!", "注册", MessageBoxButton.OK);
34               
35             }
36         }
37         public void BeginLogin(string username, string pwd, TextBlock tbIn)
38         {
39             tb = tbIn;
40             Username = username;
41             client = new LoginAndResisterClient();
42             client.QuerySQLCompleted += client_QuerySQLCompleted;
43             client.QuerySQLAsync(username, pwd);
44         }
45         void client_QuerySQLCompleted(object sender, QuerySQLCompletedEventArgs e)
46         {
47             List<Information> inforList = new List<Information>();
48             using (XmlReader xReader = XmlReader.Create(new StringReader(e.Result)))
49             {
50                 inforList.Clear();
51                 //XmlReader读取XML数据
52                 while (xReader.ReadToFollowing("Table"))
53                 {
54                     xReader.ReadToDescendant("Id");
55                     int Id = xReader.ReadElementContentAsInt();
56                     xReader.ReadToDescendant("Name");
57                     string name = xReader.ReadContentAsString();
58                     xReader.ReadToDescendant("Password");
59                     string pwd = xReader.ReadContentAsString();
60                     Information carlocation = new Information() { Id = Id, Name = name, Pwd = pwd };
61                     inforList.Add(carlocation);
62                 }
63             }
64             if (inforList.Count > 0)
65             {
66                 MessageBox.Show("登录成功!恭喜你!", "登录", MessageBoxButton.OK);
67                 tb.Text = Username+"欢迎你";
68             }
69             else
70                 MessageBox.Show("登录失败!", "登录", MessageBoxButton.OK);  
71         }
72         public class Information
73         {
74             public int Id { get; set; }
75             public string Name { get; set; }
76             public string Pwd { get; set; }
77         }
78     }
79 }
UsersManage
 1   private void btnLogin_Click(object sender, RoutedEventArgs e)
 2          {
 3              UsersManageClass usermanage = new UsersManageClass();
 4              usermanage.BeginLogin(txtName.Text.Trim(), txtPwd.Password.Trim(),tbLogin);
 5          }
 6          
 7          private void btnRegister_Click(object sender, RoutedEventArgs e)
 8          {          
 9              UsersManageClass usermanage = new UsersManageClass();
10              usermanage.BeginRegister(txtName.Text.Trim(), txtPwd.Password.Trim());
11          }
12         
调用

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data;
  4 using System.Data.SqlClient;
  5 using System.Linq;
  6 using System.Runtime.Serialization;
  7 using System.ServiceModel;
  8 using System.Text;
  9 
 10 namespace Test1.Web.APPData
 11 {
 12     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“login”。
 13     // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 login.svc 或 login.svc.cs,然后开始调试。
 14     public class Login : ILogin
 15     {
 16         SqlConnection strCon = new SqlConnection(@"server=.\MySql;database=AnJuKe_DB;user id=sa;password=123;");
 17         //server=SQLServer服务器名称;database=数据库名称;uid=用户名;pwd=密码"
 18         public void OpenSQL()
 19         {
 20             strCon.Open();
 21         }
 22 
 23         public void CloseSQL()
 24         {
 25             strCon.Close();
 26         }
 27         public string QuerySQL(string UserName,string pwd)
 28         {
 29             try
 30             {
 31                 OpenSQL();
 32                 string strSql = string.Format("select * from T_Users where Name like '%{0}%' and Password like '%{1}%'",UserName,pwd);
 33                 DataSet ds = new DataSet();
 34                 SqlDataAdapter s = new SqlDataAdapter(strSql, strCon);
 35                 s.Fill(ds);
 36                 return ds.GetXml();
 37             }
 38             catch (Exception ex)
 39             {
 40                 throw ex;
 41             }
 42             finally
 43             {
 44                 CloseSQL();
 45             }
 46         }
 47         public int  AddUsers(string Username, string pwd)
 48         {
 49           
 50             try
 51             {              
 52                 OpenSQL();
 53                 string strInsert = "insert into T_Users values('"+Username +"','"+pwd+"')";
 54                 SqlCommand cmd = new SqlCommand(strInsert, strCon);
 55              return  cmd.ExecuteNonQuery();              
 56             }
 57             catch (Exception ex)
 58             {
 59                 throw ex;
 60             }
 61             finally
 62             {
 63                 CloseSQL();               
 64             }
 65 
 66         }
 67         public int UpdateUsers(string Username, string pwd)
 68         {
 69 
 70             try
 71             {
 72                 OpenSQL();
 73                 string strUpdate = "UPDATE T_Users SET Password = '"+pwd+"' WHERE Name = '"+Username+"'";
 74                 SqlCommand cmd = new SqlCommand(strUpdate, strCon);
 75               return  cmd.ExecuteNonQuery();
 76             }
 77             catch (Exception ex)
 78             {
 79                 throw ex;
 80             }
 81             finally
 82             {
 83                 CloseSQL();
 84             }
 85         }
 86         public int DeleteUsers(string Username)
 87         {
 88             try
 89             {
 90                 OpenSQL();
 91                 string strDelete = "delete from T_Users where Name='"+Username+"'";               
 92                 SqlCommand cmd = new SqlCommand(strDelete, strCon);
 93                 return cmd.ExecuteNonQuery();
 94             }
 95             catch (Exception ex)
 96             {
 97                 throw ex;
 98             }
 99             finally
100             {
101                 CloseSQL();
102             }
103         }
104     }
105 }
增删改查

 

posted @ 2013-09-04 17:27  文刀三石  阅读(300)  评论(0编辑  收藏  举报