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.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方法
Tags:Silverlight 数据
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接