首页 > 代码库 > DAL层修改sql表数据

DAL层修改sql表数据

组长写的,DAL层Update

 1         //编辑
 2         public int Update(Dictionary<string, object> par, long uid)
 3         {
 4             if (par.Count == 0) return 0;
 5             StringBuilder Sql = new StringBuilder();
 6             Sql.Append("UPDATE uc_user SET ");
 7             SqlParameter[] pars = new SqlParameter[par.Count + 1];
 8             string[] fields = new string[par.Count];
 9             int index = 0;
10             foreach (string name in par.Keys)
11             {
12                 Sql.AppendFormat("[" + name + "] =@p_{0},", index);
13                 pars[index] = new SqlParameter("@p_" + index.ToString(), par[name]);
14                 index++;
15             }
16             Sql.Remove(Sql.Length - 1, 1);
17             Sql.Append(" WHERE ");
18             Sql.Append("[UID] = @p_" + index.ToString());
19             pars[index] = new SqlParameter("@p_" + index.ToString(), uid);
20             index++;
21             return SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.Text, Sql.ToString(), pars);
22         }

修改用户名,BLL层只需要传入对应的密码

 1         public static int Update(Dictionary<string, object> par, long AUID)
 2         {
 3             return dal.Update(par, AUID);
 4         }
 5 
 6         public static int EditPwd(string Pwd, long Auid)
 7         {
 8             Dictionary<string, object> dic = new Dictionary<string, object>();
 9             dic.Add("UserPassword", Utils.MD5(Pwd));
10             return Update(dic, Auid);
11         }

然后web层调用就行

1 BLL.Uc.UserB2B.EditPwd(newpwd, CurrentUser.UID);

因为是字典循环调用,固传入修改的什么列,就修改什么列,条件自己定义,很灵活

 

DAL层修改sql表数据