回复: Silverlight 2 (beta1)数据操作(1)
在ASP.NET工程里创建Web Service第一步:在ASP.NET工程节点上右击,选择“Add New Item...”

附件:
您所在的用户组无法下载或查看附件第二步:在弹出的对话框中,选择“Web Service”项,并命名为“UserManage.asmx”

附件:
您所在的用户组无法下载或查看附件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工程。