public int Add(QPS.NEW.Model.Enterprise model) { int checkUsername = -1; enterprise_ = model; checkUsername = Convert.ToInt32( sqlHelper_.GetSingle("select count(*) from Enterprise where Username=@username", CommandType.Text, new System.Data.SqlClient.SqlParameter[] { new System.Data.SqlClient.SqlParameter("@username", enterprise_.Username) } ) ); if (checkUsername != 0) { throw new Exception("Error:用户名已经存在"); } else { string[] filedName = new string[50]; string[] paramName = new string[50]; SqlParameter[] sqlParams = new SqlParameter[50]; int Count = 0; if (model.Username != null) { filedName[Count] = "Username"; paramName[Count] = "@" + filedName[Count]; sqlParams[Count] = new SqlParameter(paramName[Count], model.Username); Count++; } if (model.Password != null) { filedName[Count] = "Password"; paramName[Count] = "@" + filedName[Count]; string md5Pwd = MD5Helper.Encode(model.Password); sqlParams[Count] = new SqlParameter(paramName[Count], md5Pwd); Count++; } if (model.Nickname != null) { filedName[Count] = "Nickname"; paramName[Count] = "@" + filedName[Count]; sqlParams[Count] = new SqlParameter(paramName[Count], model.Nickname); Count++; } if (model.Phone != null) { filedName[Count] = "Phone"; paramName[Count] = "@" + filedName[Count]; sqlParams[Count] = new SqlParameter(paramName[Count], model.Phone); Count++; } if (model.Address != null) { filedName[Count] = "Address"; paramName[Count] = "@" + filedName[Count]; sqlParams[Count] = new SqlParameter(paramName[Count], model.Address); Count++; } if (model.Mail != null) { filedName[Count] = "Mail"; paramName[Count] = "@" + filedName[Count]; sqlParams[Count] = new SqlParameter(paramName[Count], model.Mail); Count++; } if (model.IfChecked != -999) { filedName[Count] = "IfChecked"; paramName[Count] = "@" + filedName[Count]; sqlParams[Count] = new SqlParameter(paramName[Count], model.IfChecked); Count++; } StringBuilder strSql = new StringBuilder(); strSql.Append("insert into Enterprise("); for (int i = 0; i < Count; i++) { strSql.Append(filedName[i]); if (i != Count - 1) { strSql.Append(","); } } strSql.Append(")values("); for (int i = 0; i < Count; i++) { strSql.Append(paramName[i]); if (i != Count - 1) { strSql.Append(","); } } strSql.Append(")"); int res = -1; res = sqlHelper_.ExecuteCommand( strSql.ToString(), CommandType.Text, sqlParams ); if (res != 1) { throw new Exception("Error:写入数据库失败"); } } return(1); }
public int Add(QPS.NEW.Model.Users model) { int checkUsername = -1; int res = -1; user_ = model; checkUsername = Convert.ToInt32( sqlHelper_.GetSingle("select count(*) from TUsers where UserName=@username", CommandType.Text, new System.Data.SqlClient.SqlParameter[] { new System.Data.SqlClient.SqlParameter("@username", user_.Username) } ) ); if (checkUsername != 0) { return(-1); } else { string[] filedName = new string[50]; string[] paramName = new string[50]; SqlParameter[] sqlParams = new SqlParameter[50]; int Count = 0; int userId = Convert.ToInt32(sqlHelper_.GetSingle( "select Max(UserID) from TUsers", CommandType.Text, new SqlParameter[] { new SqlParameter("@username", model.Username) } )) + 1; filedName[Count] = "UserID"; paramName[Count] = "@" + filedName[Count]; sqlParams[Count] = new SqlParameter(paramName[Count], userId); Count++; if (model.Username != null) { filedName[Count] = "UserName"; paramName[Count] = "@" + filedName[Count]; sqlParams[Count] = new SqlParameter(paramName[Count], model.Username); Count++; } if (model.Password != null) { filedName[Count] = "Pass"; paramName[Count] = "@" + filedName[Count]; string md5Pwd = MD5Helper.Encode(model.Password); sqlParams[Count] = new SqlParameter(paramName[Count], md5Pwd); Count++; } if (model.Nickname != null) { filedName[Count] = "NickName"; paramName[Count] = "@" + filedName[Count]; sqlParams[Count] = new SqlParameter(paramName[Count], model.Nickname); Count++; } StringBuilder strSql = new StringBuilder(); strSql.Append("insert into TUsers("); for (int i = 0; i < Count; i++) { strSql.Append(filedName[i]); if (i != Count - 1) { strSql.Append(","); } } strSql.Append(")values("); for (int i = 0; i < Count; i++) { strSql.Append(paramName[i]); if (i != Count - 1) { strSql.Append(","); } } strSql.Append(")"); res = sqlHelper_.ExecuteCommand( strSql.ToString(), CommandType.Text, sqlParams ); if (res != 1) { return(-2); } else { // 初始化数组 for (int i = 0; i < 50; i++) { filedName[i] = null; paramName[i] = null; sqlParams[i] = null; } Count = 0; filedName[Count] = "UserID"; paramName[Count] = "@" + filedName[Count]; sqlParams[Count] = new SqlParameter(paramName[Count], userId); Count++; if (model.Phone != null) { filedName[Count] = "PhoneNum"; paramName[Count] = "@" + filedName[Count]; sqlParams[Count] = new SqlParameter(paramName[Count], model.Phone); Count++; } if (model.Usertype != -999) { filedName[Count] = "UserType"; paramName[Count] = "@" + filedName[Count]; sqlParams[Count] = new SqlParameter(paramName[Count], model.Usertype); Count++; } strSql = new StringBuilder(); strSql.Append("insert into TUserInfo("); for (int i = 0; i < Count; i++) { strSql.Append(filedName[i]); if (i != Count - 1) { strSql.Append(","); } } strSql.Append(")values("); for (int i = 0; i < Count; i++) { strSql.Append(paramName[i]); if (i != Count - 1) { strSql.Append(","); } } strSql.Append(")"); res = -1; res = sqlHelper_.ExecuteCommand( strSql.ToString(), CommandType.Text, sqlParams ); if (res != 1) { return(-2); } if (model.Mail != null) { res = -1; res = sqlHelper_.ExecuteCommand( "insert into Web_Users(UserID,Email,Phone,Address)values(@userid,@email,@phone,@address)", CommandType.Text, new SqlParameter[] { new SqlParameter("@userid", userId), new SqlParameter("@email", model.Mail), new SqlParameter("@phone", model.Phone), new SqlParameter("@address", model.Address) } ); if (res != 1) { return(-2); } } } } return(1); }