您的位置:首页 > 数据库

Silverlight访问数据库实例二 ----- 利用Web service

2008-12-30 11:33 393 查看
Silverlight 2支持JSON、Web Service、WCF以及Sockets等新特性对数据CRUD操作,这个系列用实例结合数据库一步一步的图文描述来学习一下Silverlight 2 beta 1中进行数据库的CRUD操作方面的实战能力。一些关于Silverlight 2 Beta1的基础知识可以去看TerryLee一步一步学Silverlight 2系列文章

这篇文章介绍如何在Silverlight 2 beta 1中使用ASP.NET Web Service进行数据CRUD操作。

软件需求

Silverlight 2 (beta1)

Visual Studio 2008

SQL 2005 Express with Management Studio

在SQL 2005中创建数据库

注意:如果你已经知道如何在SQL 2005中创建数据库,请跳过此步骤看下一部分。

第一步:打开SQL Server Management Studio Express





第二步:使用Windows身份验证连接进入数据库





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





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





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





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





好了,这个表创建好了,接下来我们将使用这个表。

在Visual Studio 2008中创建 Silverlight 2 (beta1)工程

第一步:打开VS 2008创建一个新的Silverlight 2工程。





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





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





在ASP.NET工程里创建Web Service

第一步:在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面板出现一个Web Service,双击这个服务。修改Namespace为WebServiceProxy,单击OK。





现在,我们可以在Silverlight工程中使用Web Service了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: