WEB开发网
开发学院图形图像Flash Silverlight 2 (beta1)数据操作——使用ASP.NET W... 阅读

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

 2008-10-11 11:42:55 来源:WEB开发网   
核心提示: 1.CreateUser方法[WebMethod]public bool CreateUser(string userName){ try{SqlConnection _sqlConnection = new SqlConnection(); _sqlConnection.Connecti

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方法

上一页  1 2 3 4 5  下一页

Tags:Silverlight 数据

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