/// <summary> /// 更新数据,行级数据锁定 /// 返回:操作影响记录数,-1表示没有需要更新的列 /// </summary> /// <param name="NBlock_Trace_obj"></param> /// <param name="IsRowLock">是否锁行</param> /// <returns></returns> public static int Update(Trans.Db.Model.NBlock_Trace NBlock_Trace_obj, bool IsRowLock) { if (NBlock_Trace_obj.ModifiedColumns.Count <= 0) { return(-1); } StringBuilder sqlSet = new StringBuilder(); StringBuilder sqlWhere = new StringBuilder(); string sql = string.Empty; if (IsRowLock) { sql = "UPDATE NBlock_Trace WITH(ROWLOCK) SET {0} WHERE {1}"; } else { sql = "UPDATE NBlock_Trace SET {0} WHERE {1}"; } SqlCommand cmd = new SqlCommand(); cmd.Parameters.AddWithValue("@ID", NBlock_Trace_obj.ID); cmd.Parameters["@ID"].SqlDbType = System.Data.SqlDbType.BigInt; if (sqlWhere.Length > 0) { sqlWhere.Append(" AND "); } sqlWhere.Append("[ID]=@ID"); if (NBlock_Trace_obj.ModifiedColumns.Contains("[Title]")) { if (string.IsNullOrEmpty(NBlock_Trace_obj.Title)) { NBlock_Trace_obj.Title = ""; } if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[Title]=@Title"); cmd.Parameters.AddWithValue("@Title", NBlock_Trace_obj.Title); cmd.Parameters["@Title"].SqlDbType = System.Data.SqlDbType.NVarChar; } if (NBlock_Trace_obj.ModifiedColumns.Contains("[Site]")) { if (string.IsNullOrEmpty(NBlock_Trace_obj.Site)) { NBlock_Trace_obj.Site = ""; } if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[Site]=@Site"); cmd.Parameters.AddWithValue("@Site", NBlock_Trace_obj.Site); cmd.Parameters["@Site"].SqlDbType = System.Data.SqlDbType.NVarChar; } if (NBlock_Trace_obj.ModifiedColumns.Contains("[Description]")) { if (string.IsNullOrEmpty(NBlock_Trace_obj.Description)) { NBlock_Trace_obj.Description = ""; } if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[Description]=@Description"); cmd.Parameters.AddWithValue("@Description", NBlock_Trace_obj.Description); cmd.Parameters["@Description"].SqlDbType = System.Data.SqlDbType.NVarChar; } if (NBlock_Trace_obj.ModifiedColumns.Contains("[BlockCode]")) { if (string.IsNullOrEmpty(NBlock_Trace_obj.BlockCode)) { NBlock_Trace_obj.BlockCode = ""; } if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[BlockCode]=@BlockCode"); cmd.Parameters.AddWithValue("@BlockCode", NBlock_Trace_obj.BlockCode); cmd.Parameters["@BlockCode"].SqlDbType = System.Data.SqlDbType.NVarChar; } if (NBlock_Trace_obj.ModifiedColumns.Contains("[YearVal]")) { if (string.IsNullOrEmpty(NBlock_Trace_obj.YearVal)) { NBlock_Trace_obj.YearVal = ""; } if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[YearVal]=@YearVal"); cmd.Parameters.AddWithValue("@YearVal", NBlock_Trace_obj.YearVal); cmd.Parameters["@YearVal"].SqlDbType = System.Data.SqlDbType.NVarChar; } if (NBlock_Trace_obj.ModifiedColumns.Contains("[MonthVal]")) { if (string.IsNullOrEmpty(NBlock_Trace_obj.MonthVal)) { NBlock_Trace_obj.MonthVal = ""; } if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[MonthVal]=@MonthVal"); cmd.Parameters.AddWithValue("@MonthVal", NBlock_Trace_obj.MonthVal); cmd.Parameters["@MonthVal"].SqlDbType = System.Data.SqlDbType.NVarChar; } if (NBlock_Trace_obj.ModifiedColumns.Contains("[DayVal]")) { if (string.IsNullOrEmpty(NBlock_Trace_obj.DayVal)) { NBlock_Trace_obj.DayVal = ""; } if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[DayVal]=@DayVal"); cmd.Parameters.AddWithValue("@DayVal", NBlock_Trace_obj.DayVal); cmd.Parameters["@DayVal"].SqlDbType = System.Data.SqlDbType.NVarChar; } if (NBlock_Trace_obj.ModifiedColumns.Contains("[IsDel]")) { if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[IsDel]=@IsDel"); cmd.Parameters.AddWithValue("@IsDel", NBlock_Trace_obj.IsDel); cmd.Parameters["@IsDel"].SqlDbType = System.Data.SqlDbType.Int; } if (NBlock_Trace_obj.CreateTime != null && NBlock_Trace_obj.CreateTime > DateTime.MinValue && NBlock_Trace_obj.ModifiedColumns.Contains("[CreateTime]")) { if (sqlSet.Length > 0) { sqlSet.Append(","); } sqlSet.Append("[CreateTime]=@CreateTime"); cmd.Parameters.AddWithValue("@CreateTime", NBlock_Trace_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); } }
/// <summary> /// 插入数据 /// </summary> /// <returns></returns> public static bool insert(Trans.Db.Model.NBlock_Trace NBlock_Trace_obj) { SqlCommand cmd = new SqlCommand(); StringBuilder cols = new StringBuilder(); StringBuilder parameters = new StringBuilder(); string sql = "INSERT INTO NBlock_Trace({0}) values({1})"; if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[Title]"); parameters.Append("@Title"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.Title)) { cmd.Parameters.AddWithValue("@Title", NBlock_Trace_obj.Title); } else { cmd.Parameters.AddWithValue("@Title", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[Site]"); parameters.Append("@Site"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.Site)) { cmd.Parameters.AddWithValue("@Site", NBlock_Trace_obj.Site); } else { cmd.Parameters.AddWithValue("@Site", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[Description]"); parameters.Append("@Description"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.Description)) { cmd.Parameters.AddWithValue("@Description", NBlock_Trace_obj.Description); } else { cmd.Parameters.AddWithValue("@Description", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[BlockCode]"); parameters.Append("@BlockCode"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.BlockCode)) { cmd.Parameters.AddWithValue("@BlockCode", NBlock_Trace_obj.BlockCode); } else { cmd.Parameters.AddWithValue("@BlockCode", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[YearVal]"); parameters.Append("@YearVal"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.YearVal)) { cmd.Parameters.AddWithValue("@YearVal", NBlock_Trace_obj.YearVal); } else { cmd.Parameters.AddWithValue("@YearVal", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[MonthVal]"); parameters.Append("@MonthVal"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.MonthVal)) { cmd.Parameters.AddWithValue("@MonthVal", NBlock_Trace_obj.MonthVal); } else { cmd.Parameters.AddWithValue("@MonthVal", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[DayVal]"); parameters.Append("@DayVal"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.DayVal)) { cmd.Parameters.AddWithValue("@DayVal", NBlock_Trace_obj.DayVal); } else { cmd.Parameters.AddWithValue("@DayVal", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[IsDel]"); parameters.Append("@IsDel"); cmd.Parameters.AddWithValue("@IsDel", NBlock_Trace_obj.IsDel); if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[CreateTime]"); parameters.Append("@CreateTime"); if (NBlock_Trace_obj.CreateTime == null || NBlock_Trace_obj.CreateTime == DateTime.MinValue) { cmd.Parameters.AddWithValue("@CreateTime", DateTime.Now); } else { cmd.Parameters.AddWithValue("@CreateTime", NBlock_Trace_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.NBlock_Trace NBlock_Trace_obj, out Int64 ID) { ID = 0; SqlCommand cmd = new SqlCommand(); StringBuilder cols = new StringBuilder(); StringBuilder parameters = new StringBuilder(); string sql = "INSERT INTO NBlock_Trace({0}) values({1});SELECT @@IDENTITY;"; if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[Title]"); parameters.Append("@Title"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.Title)) { cmd.Parameters.AddWithValue("@Title", NBlock_Trace_obj.Title); } else { cmd.Parameters.AddWithValue("@Title", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[Site]"); parameters.Append("@Site"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.Site)) { cmd.Parameters.AddWithValue("@Site", NBlock_Trace_obj.Site); } else { cmd.Parameters.AddWithValue("@Site", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[Description]"); parameters.Append("@Description"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.Description)) { cmd.Parameters.AddWithValue("@Description", NBlock_Trace_obj.Description); } else { cmd.Parameters.AddWithValue("@Description", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[BlockCode]"); parameters.Append("@BlockCode"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.BlockCode)) { cmd.Parameters.AddWithValue("@BlockCode", NBlock_Trace_obj.BlockCode); } else { cmd.Parameters.AddWithValue("@BlockCode", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[YearVal]"); parameters.Append("@YearVal"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.YearVal)) { cmd.Parameters.AddWithValue("@YearVal", NBlock_Trace_obj.YearVal); } else { cmd.Parameters.AddWithValue("@YearVal", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[MonthVal]"); parameters.Append("@MonthVal"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.MonthVal)) { cmd.Parameters.AddWithValue("@MonthVal", NBlock_Trace_obj.MonthVal); } else { cmd.Parameters.AddWithValue("@MonthVal", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[DayVal]"); parameters.Append("@DayVal"); if (!string.IsNullOrEmpty(NBlock_Trace_obj.DayVal)) { cmd.Parameters.AddWithValue("@DayVal", NBlock_Trace_obj.DayVal); } else { cmd.Parameters.AddWithValue("@DayVal", ""); } if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[IsDel]"); parameters.Append("@IsDel"); cmd.Parameters.AddWithValue("@IsDel", NBlock_Trace_obj.IsDel); if (cols.Length > 0) { cols.Append(","); parameters.Append(","); } cols.Append("[CreateTime]"); parameters.Append("@CreateTime"); if (NBlock_Trace_obj.CreateTime == null || NBlock_Trace_obj.CreateTime == DateTime.MinValue) { cmd.Parameters.AddWithValue("@CreateTime", DateTime.Now); } else { cmd.Parameters.AddWithValue("@CreateTime", NBlock_Trace_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); ID = Convert.ToInt64(idobj); if (ID == 0) { b = false; } } catch { b = false; } return(b); }