Silverlight 2 (beta1)数据操作(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文件的 <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了,接下来,我还是一步一步展示如何使用Web Service查询数据。

这一篇就写到这里,下一篇继续完成这个实例。

下一篇包含以下内容:

  • 添加数据部分
  • 查询数据部分
  • 修改数据部分
  • 删除数据部分
  • 整合程序
  • 结语
  • 例子下载

版权声明:本文为博主http://www.zuiniusn.com 原创文章,未经博主允许不得转载。

时间: 2024-08-14 21:25:14

Silverlight 2 (beta1)数据操作(1)——使用ASP.NET Web Service进行数据CRUD操作(上)的相关文章

Silverlight 2 (beta1)数据操作(2)——使用ASP.NET Web Service进行数据CRUD操作(下)

前台界面 后台代码 //按钮事件 void saveButton_Click(object sender, RoutedEventArgs e) { if (userName.Text.Trim() == string.Empty) { errMessage.Foreground = new SolidColorBrush(Colors.Red); errMessage.Text = "请输入用户名称!"; errMessage.Visibility = Visibility.Visi

HYAppFrame数据库开发入门(ASP.NET Web Service)

本节主要讲解服务器端ASP.NET Web Service数据库配置和操作,客户端数据库操作. HYAppFrame项目地址:https://sourceforge.net/u/chinahysoft/profile/ 1.    服务器端数据库操作 HYAppFrame服务器端通过ASP.NET Web Service连接数据库. 数据库连接配置 在文件Web.config配置数据库,需设置数据库服务器地址Data Source.数据库名称Initial Catalog.访问帐号User Id

Win Form + ASP.NET Web Service 文件上传下载--HYAppFrame

本章节主要讲解HYAppFrame服务器端如何ASP.NET Web Service实现文件(含大文件)上传,WinForm客户端如何下载文件. 1    服务器端文件上传 1.1 上传文件 函数FileUpload(stringfileFullPath, byte[] file)用于上传文件,生成文件前检查文件路径所在文件夹是否存在,不存在则首先创建文件夹. [WebMethod(EnableSession = true,Description = "上传文件")] public i

Part 17 Consuming ASP NET Web Service in AngularJS using $http

Here is what we want to do1. Create an ASP.NET Web service. This web service retrieves the data from SQL Server database table, returns it in JSON formt.2. Call the web service using AngularJS and display employee data on the web page Step 1 : Create

在 Visual Studio 2010 中创建 ASP.Net Web Service

第一步:创建一个“ASP.Net Empty Web Application”项目 第二步:在项目中添加“Web Service”新项目 第一步之后,Visual Studio 2010会创建一个仅含一个站点配制文件(Web.config)的空站点,其余的什么也没有. 我们在Visual Studio 2010的Solution Explorer中,选中当前的这个project,添加新项目(右键菜单:Add --> New Item),选择“Web Service”这种类型: 看到这里读者应该就

ASP.NET Web Service中使用Session 及 Session丢失解决方法 续

原文:ASP.NET Web Service中使用Session 及 Session丢失解决方法 续 1.关于Session丢失问题的说明汇总,参考这里 2.在Web Servcie中使用Session,需要对Web Method做如下处理 [WebMethod(EnableSession = true)]public void usingSession(){    Session["Name"] = "Name";} 如果不加EnableSession = tru

微软实战训练营(X)重点班第(1)课:SOA必备知识之ASP.NET Web Service开发实战

微软实战训练营 上海交大(A)实验班.(X)重点班 内部课程资料 链接:http://pan.baidu.com/s/1jGsTjq2 密码:0wmf <微软实战训练营(X)重点班第(1)课:SOA必备知识之ASP.NET Web Service开发实战>微软实战训练营 上海交大(A)实验班.(X)重点班 .(E)英语口语班http://54peixun.com/MSTrainingCamp/index.html 微软实战训练营(X)重点班第(1)课:SOA必备知识之ASP.NET Web S

sharepoint 2010 创建自定义的ASP.NET Web Service (上)

项目背景 根据客户需求在SharePoint 2010 中创建自定义的ASP.NET Web Service可以分为3种方式(我所知道的).废话少说,下面一一列举: 创建方式 MSDN 官方博客自己的一个创建ASP.NET Web Service.http://msdn.microsoft.com/zh-cn/library/ms464040(v=office.14).aspx 但是它不推荐这种方式. 通过创建类库项目,使类库项目包装webservice. MSDN推荐使用的方式,通过WCF创建

asp.net Web 项目的文件/文件夹上传下载

以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传  可以带参数 [HttpPost("upload")] public JsonResult uploadProject(IFormFile file, string userId) { if (file != null) { var fileDir = "D:\\aaa"