首页 > 代码库 > 用WebService实现对数据库进行操作(添加+删除+修改)(转)
用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 UserName=@UserName,UsersPwd=@UsersPwd where id=@id";
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 id=@id";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter pari = new SqlParameter("@id", id);
cmd.Parameters.Add(pari);
int result = cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();
return result;
}
用WebService实现对数据库进行操作(添加+删除+修改)(转)