微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight2数据操作(1)——使用ASP.NET Web Service进行数据CRUD操作(上) - 星辰技术社区


目录

  • 导言
  • 软件需求
  • sql 2005中创建数据库
  • 在Visual Studio 2008中创建 Silverlight 2 (beta1)工程
  • 在ASP.NET工程里创建Web Service
  • 在Silverlight 2 (beta1)工程中引用ASP.NET Web Service
  • 添加数据部分
  • 查询数据部分
  • 修改数据部分
  • 删除数据部分
  • 整合程序
  • 结语

导言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文件 标签添加数据库连接。
< connectionStrings >
    <
add name =" sqlConnectionString" connectionString =" Data Source=./sqlEXPRESS;Initial Catalog=YJingLeeDB;Integrated Security=True" />
connectionStrings>
第四步:编辑ProductManager.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(" ");
        sb.Append(" " );
        foreach (DaTarow dr in ds.Tables[0].Rows)
        {
            sb.Append(" " );
            sb.Append(" " );
            sb.Append(dr[0].ToString());
            sb.Append("");
            sb.Append(" " );
            sb.Append(dr[1].ToString());
            sb.Append("");
            sb.Append("");
        }
        sb.Append("");
          _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了,接下来,我还是一步一步展示如何使用Web Service查询数据。
这一篇就写到这里,下一篇继续完成这个实例。
下一篇包含以下内容

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐