Silverlight访问数据库实例二 ----- 利用Web service
第二步:使用Windows身份验证连接进入数据库

第三步:在对象资源管理器窗口的数据库节点上右击选择“新建数据库...”

第四步:输入数据库名称(我命名为“YJingLeeDB”),然后单击“确定”按钮。

第五步:在刚刚创建数据库的表节点上右击选择“新建表...”

第六步:创建一个User表,新建2列,分别为UserID(主键)和UserName。

好了,这个表创建好了,接下来我们将使用这个表。
在Visual Studio 2008中创建 Silverlight 2 (beta1)工程第一步:打开VS 2008创建一个新的Silverlight 2工程。

第二步:选择创建一个ASP.NET Web Site或者Web ApplicationProject用来托管Silverlight应用程序。

第三步:创建完成后的项目结构如下所示:

第一步:在ASP.NET工程节点上右击,选择“Add New Item...”

第二步:在弹出的对话框中,选择“Web Service”项,并命名为“UserManage.asmx”

第三步:在web.config文件的<configuration>标签下添加数据库连接。
<connectionStrings><add name="sqlConnectionString"connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=YJingLeeDB;Integrated Security=True"/></connectionStrings>
第四步:编辑UserManager.asmx文件,分别编写CRUD四个方法。
1.CreateUser方法
[WebMethod]public bool CreateUser(string userName){try{SqlConnection _sqlConnection = new SqlConnection();_sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["sqlConnectionString"].ToString();_sqlConnection.Open();SqlCommand command = new SqlCommand();command.Connection = _sqlConnection;command.CommandType = CommandType.Text;command.CommandText ="INSERT INTO [User] ([UserName]) VALUES ('" +userName.ToString().Replace("'", "''") + "')";command.ExecuteNonQuery();_sqlConnection.Close();return true;}catch (Exception ex){return false;}}2.RetrieveUser方法
[WebMethod]public string RetrieveUsers(){try{SqlConnection _sqlConnection = new SqlConnection();_sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["sqlConnectionString"].ToString();_sqlConnection.Open();SqlDataAdapter da = new SqlDataAdapter();da.SelectCommand = new SqlCommand("SELECT * FROM [User]", _sqlConnection);DataSet ds = new DataSet();da.Fill(ds);StringBuilder sb = new StringBuilder();sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");sb.Append("<Users>");foreach (DataRow dr in ds.Tables[0].Rows){sb.Append("<User>");sb.Append("<UserID>");sb.Append(dr[0].ToString());sb.Append("</UserID>");sb.Append("<UserName>");sb.Append(dr[1].ToString());sb.Append("</UserName>");sb.Append("</User>");}sb.Append("</Users>");_sqlConnection.Close();return sb.ToString();}catch (Exception ex){return string.Empty;}}3.UpdateUser方法
[WebMethod]public bool UpdateUser(int userID, string userName){try{SqlConnection _sqlConnection = new SqlConnection();_sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["sqlConnectionString"].ToString();_sqlConnection.Open();SqlCommand command = new SqlCommand();command.Connection = _sqlConnection;command.CommandType = CommandType.Text;command.CommandText = "UPDATE [User] " +"SET [UserName] = '" +userName.ToString().Replace("'", "''") + "'" +"WHERE [UserID] = " + userID.ToString();command.ExecuteNonQuery();_sqlConnection.Close();return true;}catch (Exception ex){return false;}}4.DeleteUser方法
[WebMethod]public bool DeleteUser(int userID){try{SqlConnection _sqlConnection = new SqlConnection();_sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["sqlConnectionString"].ToString();_sqlConnection.Open();SqlCommand command = new SqlCommand();command.Connection = _sqlConnection;command.CommandType = CommandType.Text;command.CommandText ="DELETE [User] WHERE [UserID] = " + userID.ToString();command.ExecuteNonQuery();_sqlConnection.Close();return true;}catch (Exception ex){return false;}}第五步:修改ASP.NET工程属性,修改一个固定的端口。

第六步:编译ASP.NET工程。
在Silverlight 2 (beta1)工程中引用ASP.NET Web Service第一步:在Silverlight工程的引用节点上右击选择“Add Service Reference...”。

第二步:在下面的对话框中点击“Discover”按钮

第三步:在点击Discover按钮之后,地址栏里显示了UserManage.asmx。在Service面板出现一个WebService,双击这个服务。修改Namespace为WebServiceProxy,单击OK。

现在,我们可以在Silverlight工程中使用Web Service了