用WebService實現對數據庫進行操作(添加+刪除+修改)(轉)
轉自:http://blog.csdn.net/beyondqd/article/details/6703169
表為User,字段有 編號: int id,用戶名:string UserName,密碼:string UserPwd
/// <summary>
/// 添加
/// </summary>
/// <param name="name">用戶名</param>
/// <param name="pwd">密碼</param>
/// <returns></returns>
[WebMethod]
public int Add(string name,string pwd)
{
SqlConnection conn = new SqlConnection("連接字符串");
conn.Open();
string sql = "intsert into User (UserName,UsersPwd) values(@UserName,@UsersPwd)";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter parn = new SqlParameter("@UserName", name);
cmd.Parameters.Add(parn);
SqlParameter parp = new SqlParameter("@UsersPwd", pwd);
cmd.Parameters.Add(parp);
int result = cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();
return result;
}
/// <summary>
/// 修改
/// </summary>
/// <param name="name">用戶名</param>
/// <param name="pwd">密碼</param>
/// <param name="id">編號</param>
/// <returns></returns>
[WebMethod]
public int Update(string name, string pwd, int id)
{
SqlConnection conn = new SqlConnection("連接字符串");
conn.Open();
string sql = "update User set [email protected]
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter parn = new SqlParameter("@UserName", name);
cmd.Parameters.Add(parn);
SqlParameter parp = new SqlParameter("@UsersPwd", pwd);
cmd.Parameters.Add(parp);
SqlParameter pari = new SqlParameter("@id", id);
cmd.Parameters.Add(pari);
int result = cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();
return result;
}
/// <summary>
/// 刪除
/// </summary>
/// <param name="id">編號</param>
/// <returns></returns>
[WebMethod]
public int Delete(int id)
{
SqlConnection conn = new SqlConnection("連接字符串");
conn.Open();
string sql = "delete from User where [email protected]