/// <summary> /// SqlCommand /// </summary> /// <param name="conn"></param> /// <param name="execType"></param> /// <returns></returns> internal SqlCommand GetSqlCommand(string conn, EnumExecType execType) { if (SqlCommands == null) { SqlCommands = new Dictionary <string, SqlCommand>(); } else { if (SqlCommands.ContainsKey(conn + "|" + execType.ToString())) { return(SqlCommands[conn + "|" + execType.ToString()]); } } SqlConnection con = new SqlConnection(conn); SqlCommand cmd = new SqlCommand(); con.Open(); cmd.Connection = con; if (execType == EnumExecType.ExecSql || execType == EnumExecType.ExecSqlForBatch) { cmd.CommandType = CommandType.Text; } else if (execType == EnumExecType.ExecProc) { cmd.CommandType = CommandType.StoredProcedure; } else { cmd.CommandType = CommandType.Text; } cmd.CommandTimeout = 3000; SqlCommands.Add(conn + "|" + execType.ToString(), cmd); return(cmd); }
/// <summary> /// GetSqlCommand /// </summary> /// <param name="conn"></param> /// <param name="execType"></param> /// <returns></returns> internal OracleCommand GetSqlCommand(string conn, EnumExecType execType) { OracleConnection con = new OracleConnection(conn); OracleCommand cmd = new OracleCommand(); con.Open(); cmd.Connection = con; if (execType == EnumExecType.ExecSql || execType == EnumExecType.ExecSqlForBatch) { cmd.CommandType = CommandType.Text; } else if (execType == EnumExecType.ExecProc) { cmd.CommandType = CommandType.StoredProcedure; } else { return(null); } cmd.CommandTimeout = 3000; return(cmd); }
/// <summary> /// ExecSQLForBatch /// </summary> /// <param name="cmd"></param> /// <param name="sql"></param> /// <param name="objValuesArr"></param> /// <param name="objDbTypes"></param> /// <returns></returns> public int ExecSqlForBatch(string conn, EnumExecType execType, string sql, ref int step, object[][] objValuesArr, params DbType[] objDbTypes) { //EnumExecType execType = EnumExecType.ExecSql; //if (typeStr == "1") // execType = EnumExecType.ExecSql; //else if (typeStr == "2") // execType = EnumExecType.ExecSqlForBatch; //else if (typeStr == "3") // execType = EnumExecType.ExecSqlForBatchSimpleInsert; //else if (typeStr == "4") // execType = EnumExecType.ExecProc; // 2100参数限制 int divNum = 2100; SqlCommand[] sqlCommandArr = null; if (objValuesArr[0].Length * objDbTypes.Length > 2100) { sqlCommandArr = new SqlCommand[((objValuesArr[0].Length * objDbTypes.Length) / 2100) + 1]; for (int m = 0; m < sqlCommandArr.Length; m++) { sqlCommandArr[m] = GetSqlCommand(conn, execType); } } else { sqlCommandArr = new SqlCommand[1]; sqlCommandArr[0] = GetSqlCommand(conn, execType); } int intAffectedRows = 0; int num = 0; try { SqlDbType[] enmSqlDbType = ConvertDbTypeToSqlDbType(objDbTypes); SqlParameter param = null; StringBuilder sb = new StringBuilder(); for (int i = 0; i < objValuesArr[0].Length; i++) { sb.Append("---> " + Convert.ToString(i + 1) + Environment.NewLine); for (int j = 0; j < objValuesArr.Length; j++) { if (objValuesArr[j][i] == null) { objValuesArr[j][i] = System.DBNull.Value; } sb.Append(Convert.ToString(j + 1) + ":= " + objValuesArr[j][i] + "; "); } sb.AppendLine(); } int div = 0; for (int i = 0; i < objValuesArr[0].Length; i++) { for (int j = 0; j < enmSqlDbType.Length; j++) { div = ((i + 1) * enmSqlDbType.Length) / divNum; if (j + 1 < 10) { param = new SqlParameter(Convert.ToString((i + step) * 10) + Convert.ToString(j + 1), enmSqlDbType[j]); } else { param = new SqlParameter(Convert.ToString(i + step) + Convert.ToString(j + 1), enmSqlDbType[j]); } //param = new SqlParameter(Convert.ToString((i + step) * 10) + Convert.ToString(j + 1), enmSqlDbType[j]); param.Direction = ParameterDirection.Input; param.Value = objValuesArr[j][i]; sqlCommandArr[div].Parameters.Add(param); } sqlCommandArr[div].CommandText = GetParm(sql, (i + step) * 10, (i == 0)); if (i == 0) { SqlLog.OutPutSql("values: " + sb.ToString()); } if (sqlCommandArr[div].Connection.State == ConnectionState.Closed) { sqlCommandArr[div].Connection.Open(); } intAffectedRows += sqlCommandArr[div].ExecuteNonQuery(); num = i + step; } } catch (System.Exception objEx) { intAffectedRows = -1; throw objEx; } finally { step = num + 1; } return(intAffectedRows); }
/// <summary> /// 批量操作 /// </summary> /// <param name="p_strConnstr"></param> /// <param name="p_strSQL"></param> /// <param name="p_objValuesArr"></param> /// <param name="p_lngAffectedRows"></param> /// <param name="p_objDbTypes"></param> /// <returns></returns> public int ExecSqlForBatch(string conn, EnumExecType execType, string sql, ref int step, object[][] objValuesArr, params DbType[] objDbTypes) { OracleCommand cmd = null; if (execType == EnumExecType.ExecSql || execType == EnumExecType.ExecSqlForBatch || execType == EnumExecType.ExecSqlForBatchSimpleInsert) { cmd = GetSqlCommand(conn, CommandType.Text); } else if (execType == EnumExecType.ExecProc) { cmd = GetSqlCommand(conn, CommandType.StoredProcedure); } else { cmd = GetSqlCommand(conn, CommandType.Text); } int intAffectedRows = 0; try { cmd.ArrayBindCount = objValuesArr[0].Length; cmd.CommandText = GetParam(sql); OracleDbType[] enmOracleDbType = ConvertDbTypeToOracleDbType(objDbTypes); OracleParameter param = null; for (int i = 0; i < enmOracleDbType.Length; i++) { param = new OracleParameter(Convert.ToString(i + 1 + step * 10), enmOracleDbType[i]); param.Direction = ParameterDirection.Input; param.Value = objValuesArr[i]; cmd.Parameters.Add(param); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < objValuesArr[0].Length; i++) { sb.Append("---> " + Convert.ToString(i + 1) + Environment.NewLine); for (int j = 0; j < objValuesArr.Length; j++) { if (objValuesArr[j][i] == null) { objValuesArr[j][i] = System.DBNull.Value; } sb.Append(Convert.ToString(j + 1) + ":= " + objValuesArr[j][i] + "; "); } sb.AppendLine(); } SqlLog.OutPutSql("Values: " + sb.ToString()); if (cmd.Connection.State == ConnectionState.Closed) { cmd.Connection.Open(); } intAffectedRows = cmd.ExecuteNonQuery(); } catch (System.Exception objEx) { intAffectedRows = -1; throw objEx; } finally { if (cmd != null) { cmd.Connection.Close(); cmd.Dispose(); } } return(intAffectedRows); }