WEB开发网
开发学院图形图像Flash Silverlight 2 (beta1)数据操作(4)——调用WCF进行... 阅读

Silverlight 2 (beta1)数据操作(4)——调用WCF进行数据CRUD操作

 2008-10-11 11:42:33 来源:WEB开发网   
核心提示: 编写WCF Service第一步:定义服务契约,我们为WCF Service提供的增删查改方法定义服务契约,Silverlight 2 (beta1)数据操作(4)——调用WCF进行数据CRUD操作(2),[ServiceContract]public interface IUserSer

编写WCF Service

第一步:定义服务契约。我们为WCF Service提供的增删查改方法定义服务契约。

[ServiceContract]
public interface IUserService
{
  [OperationContract]
  string RetrieveUser();
  [OperationContract]
  bool CreateUser(string userName);
  [OperationContract]
  bool UpdateUser(int userID, string userName);
  [OperationContract]
  bool DeleteUser(int userID);
}

第二步:在UserService中实现服务:为增删查改四个方法具体实现。这里使用SQL语句了,同第一篇类似。

public class UserService : IUserService
{
  //查询用户
  public string RetrieveUser()
  {
    try
    {
      SqlConnection _sqlConnection =
        new SqlConnection( ConfigurationManager.
        ConnectionStrings["sqlConnectionString"].
        ConnectionString);
      _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;
    }
  }
  //创建用户
  public bool CreateUser(string userName)
  {
    try
    {
      SqlConnection _sqlConnection =
        new SqlConnection(ConfigurationManager.
        ConnectionStrings["sqlConnectionString"]
        .ConnectionString);
      _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;
    }
  }
  //更新用户
  public bool UpdateUser(int userID, string userName)
  {
    try
    {
      SqlConnection _sqlConnection =
        new SqlConnection(ConfigurationManager.
        ConnectionStrings["sqlConnectionString"]
        .ConnectionString);
      _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;
    }
  }
  //删除用户
  public bool DeleteUser(int userID)
  {
    try
    {
      SqlConnection _sqlConnection =
        new SqlConnection(ConfigurationManager.
        ConnectionStrings["sqlConnectionString"]
        .ConnectionString);
      _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;
    }
  }
}

第三步:修改Web.config中的服务配置。

上一页  1 2 3 4 5 6 7  下一页

Tags:Silverlight 数据

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接