/// <summary> /// 插入数据 /// </summary> /// <returns></returns> public static bool insert(Trans.Db.Model.NUser_Info NUser_Info_obj) { SqlCommand cmd = new SqlCommand(); StringBuilder cols = new StringBuilder(); StringBuilder parameters = new StringBuilder(); string sql = "INSERT INTO NUser_Info({0}) values({1})"; if (!string.IsNullOrEmpty(NUser_Info_obj.UserName)) { if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[UserName]"); parameters.Append("@UserName"); cmd.Parameters.AddWithValue("@UserName", NUser_Info_obj.UserName); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[Password]"); parameters.Append("@Password"); if (!string.IsNullOrEmpty(NUser_Info_obj.Password)) { cmd.Parameters.AddWithValue("@Password", NUser_Info_obj.Password); } else { cmd.Parameters.AddWithValue("@Password", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[RefUserId]"); parameters.Append("@RefUserId"); cmd.Parameters.AddWithValue("@RefUserId", NUser_Info_obj.RefUserId); if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[Status]"); parameters.Append("@Status"); cmd.Parameters.AddWithValue("@Status", NUser_Info_obj.Status); if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[IsDel]"); parameters.Append("@IsDel"); cmd.Parameters.AddWithValue("@IsDel", NUser_Info_obj.IsDel); if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[CreateTime]"); parameters.Append("@CreateTime"); if (NUser_Info_obj.CreateTime == null || NUser_Info_obj.CreateTime == DateTime.MinValue) { cmd.Parameters.AddWithValue("@CreateTime", DateTime.Now); } else { cmd.Parameters.AddWithValue("@CreateTime", NUser_Info_obj.CreateTime); } sql = string.Format(sql, cols.ToString(), parameters.ToString()); cmd.CommandText = sql; bool b = true; try { int QueryCount = new Trans.Db.DBUtility.DBHelper(false).ExecNonQuery(cmd); if (QueryCount < 1) { b = false; } } catch { b = false; } return(b); }
/// <summary> /// 插入数据,返回自增列ID /// </summary> /// <returns></returns> public static bool Add(Trans.Db.Model.NUser_Info NUser_Info_obj, out Int32 UserId) { UserId = 0; SqlCommand cmd = new SqlCommand(); StringBuilder cols = new StringBuilder(); StringBuilder parameters = new StringBuilder(); string sql = "INSERT INTO NUser_Info({0}) values({1});SELECT @@IDENTITY;"; if (!string.IsNullOrEmpty(NUser_Info_obj.UserName)) { if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[UserName]"); parameters.Append("@UserName"); cmd.Parameters.AddWithValue("@UserName", NUser_Info_obj.UserName); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[Password]"); parameters.Append("@Password"); if (!string.IsNullOrEmpty(NUser_Info_obj.Password)) { cmd.Parameters.AddWithValue("@Password", NUser_Info_obj.Password); } else { cmd.Parameters.AddWithValue("@Password", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[RefUserId]"); parameters.Append("@RefUserId"); cmd.Parameters.AddWithValue("@RefUserId", NUser_Info_obj.RefUserId); if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[Status]"); parameters.Append("@Status"); cmd.Parameters.AddWithValue("@Status", NUser_Info_obj.Status); if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[IsDel]"); parameters.Append("@IsDel"); cmd.Parameters.AddWithValue("@IsDel", NUser_Info_obj.IsDel); if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[CreateTime]"); parameters.Append("@CreateTime"); if (NUser_Info_obj.CreateTime == null || NUser_Info_obj.CreateTime == DateTime.MinValue) { cmd.Parameters.AddWithValue("@CreateTime", DateTime.Now); } else { cmd.Parameters.AddWithValue("@CreateTime", NUser_Info_obj.CreateTime); } sql = string.Format(sql, cols.ToString(), parameters.ToString()); cmd.CommandText = sql; bool b = true; try { object idobj = new Trans.Db.DBUtility.DBHelper(false).ExecScalar(cmd); UserId = Convert.ToInt32(idobj); if (UserId == 0) { b = false; } } catch { b = false; } return(b); }
/// <summary> /// 更新数据,行级数据锁定 /// 返回:操作影响记录数,-1表示没有需要更新的列 /// </summary> /// <param name="NUser_Info_obj"></param> /// <param name="IsRowLock">是否锁行</param> /// <returns></returns> public static int Update(Trans.Db.Model.NUser_Info NUser_Info_obj, bool IsRowLock) { if (NUser_Info_obj.ModifiedColumns.Count <= 0) { return(-1); } StringBuilder sqlSet = new StringBuilder(); StringBuilder sqlWhere = new StringBuilder(); string sql = string.Empty; if (IsRowLock) { sql = "UPDATE NUser_Info WITH(ROWLOCK) SET {0} WHERE {1}"; } else { sql = "UPDATE NUser_Info SET {0} WHERE {1}"; } SqlCommand cmd = new SqlCommand(); cmd.Parameters.AddWithValue("@UserId", NUser_Info_obj.UserId); cmd.Parameters["@UserId"].SqlDbType = System.Data.SqlDbType.Int; if (sqlWhere.Length > 0) { sqlWhere.Append(" AND "); } sqlWhere.Append("[UserId]=@UserId"); if (NUser_Info_obj.ModifiedColumns.Contains("[UserName]")) { if (string.IsNullOrEmpty(NUser_Info_obj.UserName)) { NUser_Info_obj.UserName = ""; } if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[UserName]=@UserName"); cmd.Parameters.AddWithValue("@UserName", NUser_Info_obj.UserName); cmd.Parameters["@UserName"].SqlDbType = System.Data.SqlDbType.NVarChar; } if (NUser_Info_obj.ModifiedColumns.Contains("[Password]")) { if (string.IsNullOrEmpty(NUser_Info_obj.Password)) { NUser_Info_obj.Password = ""; } if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[Password]=@Password"); cmd.Parameters.AddWithValue("@Password", NUser_Info_obj.Password); cmd.Parameters["@Password"].SqlDbType = System.Data.SqlDbType.NVarChar; } if (NUser_Info_obj.ModifiedColumns.Contains("[RefUserId]")) { if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[RefUserId]=@RefUserId"); cmd.Parameters.AddWithValue("@RefUserId", NUser_Info_obj.RefUserId); cmd.Parameters["@RefUserId"].SqlDbType = System.Data.SqlDbType.Int; } if (NUser_Info_obj.ModifiedColumns.Contains("[Status]")) { if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[Status]=@Status"); cmd.Parameters.AddWithValue("@Status", NUser_Info_obj.Status); cmd.Parameters["@Status"].SqlDbType = System.Data.SqlDbType.Int; } if (NUser_Info_obj.ModifiedColumns.Contains("[IsDel]")) { if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[IsDel]=@IsDel"); cmd.Parameters.AddWithValue("@IsDel", NUser_Info_obj.IsDel); cmd.Parameters["@IsDel"].SqlDbType = System.Data.SqlDbType.Int; } if (NUser_Info_obj.CreateTime != null && NUser_Info_obj.CreateTime > DateTime.MinValue && NUser_Info_obj.ModifiedColumns.Contains("[CreateTime]")) { if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[CreateTime]=@CreateTime"); cmd.Parameters.AddWithValue("@CreateTime", NUser_Info_obj.CreateTime); cmd.Parameters["@CreateTime"].SqlDbType = System.Data.SqlDbType.DateTime2; } sql = string.Format(sql, sqlSet.ToString(), sqlWhere.ToString()); cmd.CommandText = sql; try { return(new Trans.Db.DBUtility.DBHelper(false).ExecNonQuery(cmd)); } catch { return(-1); } }