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