/// <summary> /// 执行特定SQL语句 /// </summary> /// <param name="parameters">参数</param> /// <param name="sqlName">SQL名称</param> /// <returns>受影响的行数</returns> public int ExecuteNonQuery(HashObject parameters, string sqlName, SqlType sqlType = SqlType.SqlName) { using (DbHelperWrapper dbhelper = new DbHelperWrapper(this._dbrwType, this._dbName, this._tableName, sqlName, parameters, sqlType)) { dbhelper.LogEnable = this.LogEnable; int rows = dbhelper.ExecuteNonQuery(); return(rows); } }
///// <summary> ///// 批量执行SQL语句(组织成一个大的SQL语句,建议尽量只包含insert语句) ///// </summary> ///// <param name="sqlDataList"></param> ///// <returns></returns> //public int ExecuteBatch(List<DALObject> sqlDataList) //{ // if (sqlDataList.Count == 0) // return 0; // StringBuilder cmdtext = new StringBuilder(); // //组织SQL语句 // HashObject parameters = new HashObject(); // parameters["profileid"] = sqlDataList[0].Parameters["profileid"]; // foreach (DALObject obj in sqlDataList) // { // string sql = string.Empty; // if (obj.ExSqlType == SqlType.SqlName) // { // SqlObj sqlobj = SchemaManager.GetDefaultSQL(this._dbName, this._tableName, obj.SqlName); // sql = sqlobj.SqlText; // } // if (obj.Parameters != null && obj.Parameters.Count > 0) // { // string oneitemvalue = string.Empty; // foreach (KeyValuePair<string, object> oneitem in obj.Parameters) // { // if (oneitem.Value != null) // { // if (this._allColumns.ContainsKey(oneitem.Key) && _varcharTypes.Contains(this._allColumns[oneitem.Key].Type)) // oneitemvalue = string.Format("'{0}'", oneitem.Value.ToString()); // else // oneitemvalue = oneitem.Value.ToString(); // } // sql = sql.Replace("@" + oneitem.Key, oneitemvalue); // } // } // cmdtext.Append(sql); // } // using (DbHelperWrapper dbhelper = new DbHelperWrapper(this._dbrwType, this._dbName, this._tableName, cmdtext.ToString(), parameters, SqlType.CmdText)) // { // int rows = dbhelper.ExecuteNonQuery(); // return rows; // } //} /// <summary> /// 删除数据(根据主键) /// </summary> /// <param name="parameters">参数(需包含主键)</param> /// <returns></returns> public bool Delete(HashObject parameters, string sqlName = "delete") { //this.CheckForPrimary(parameters);//判断参数中是否包含主键列,如果不包含,抛出异常 using (DbHelperWrapper dbhelper = new DbHelperWrapper(this._dbrwType, this._dbName, this._tableName, sqlName, parameters)) { dbhelper.LogEnable = this.LogEnable; int rows = dbhelper.ExecuteNonQuery(); return(rows > 0); } }
/// <summary> /// 批量insert语句提交 /// </summary> /// <param name="maxCount">最大拼接条数(默认100)</param> /// <returns></returns> public int BatchInsertComplete(int maxCount = 100) { if (this._insertBatchList.Count == 0) { return(0); } //if (this == DALBase.Create(this._dbName, this._tableName, this._dbrwType)) //{ // throw new CCError("批量插入的时候,dalbase需要传入ifforbatch参数"); //} //int rows = this.ExecuteBatch(this._insertBatchList); //return rows; int rows = 0; HashObject parameters = new HashObject(); parameters["profileid"] = this._profileid; if (_totalNeedNewId > 0) { parameters[_primaryColumn] = _firstNewId > 0 ? _firstNewId : (_firstNewId = GetIdentity(_totalNeedNewId)[0]); } int times = (int)Math.Ceiling(this._insertBatchList.Count / (maxCount * 1.0)); for (int i = 0; i < times; i++) { List <string> arrTemp = new List <string>(); for (int j = 0; j < maxCount; j++) { int index = i * maxCount + j; if (index >= this._insertBatchList.Count) { break; } arrTemp.Add(this._insertBatchList[index]); } string insertSql = this.GetBatchInsertSql(arrTemp); using (DbHelperWrapper dbhelper = new DbHelperWrapper(this._dbrwType, this._dbName, this._tableName, insertSql, parameters, SqlType.CmdText)) { dbhelper.LogEnable = this.LogEnable; int _rows = dbhelper.ExecuteNonQuery(); rows = rows + _rows; } } this._profileid = 0; this._insertBatchList = null; _totalNeedNewId = 0; _firstNewId = 0; return(rows); }
/// <summary> /// 插入(返回自增列的值) /// </summary> /// <param name="parameters">参数</param> /// <returns>如果外部已经获取自增列,则返回-1,否则,返回自增列的值</returns> public long Insert(HashObject parameters) { if (parameters == null || parameters.Count == 0) { throw new ArgumentNullException("parameters 不能为空!"); } long result = -1; HashObject newParameters = CreateNewInsertParametersAutoId(parameters, out result); using (DbHelperWrapper dbhelper = new DbHelperWrapper(this._dbrwType, this._dbName, this._tableName, "insert", newParameters)) { dbhelper.LogEnable = this.LogEnable; dbhelper.ExecuteNonQuery(); } return(result); }
/// <summary> /// 根据特定列的值更新 /// </summary> /// <param name="setParameters">需要更新的列和值</param> /// <param name="whereParameters">where 条件列和值</param> /// <param name="offsetParameters">需要偏移更新的参数列update set a = a + @a</param> /// <returns></returns> public int UpdateByWhere(HashObject setParameters, HashObject whereParameters, string[] offsetParameters = null) { if (setParameters == null || setParameters.Count == 0) { throw new ArgumentNullException("setParameters 不能为空!"); } if (whereParameters == null || whereParameters.Count == 0) { throw new ArgumentNullException("whereParameters 不能为空!"); } HashObject newParameters; string sql = SchemaManager.GetUpdateSQL(this._dbName, this._tableName, setParameters, whereParameters, offsetParameters, out newParameters); using (DbHelperWrapper dbhelper = new DbHelperWrapper(this._dbrwType, this._dbName, this._tableName, sql, newParameters, SqlType.CmdText)) { int rows = dbhelper.ExecuteNonQuery(); return(rows); } }