using Dm; using Kdbndp; using KdbndpTypes; using System; using System.Data; using System.Data.Common; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace Lskj.Core { /// /// 数据库连接类型 /// public enum ConnectionType { SqlServer = 0, KdbnServer = 1, DmServer = 2 } public static class SqlHelper { /// /// 连接类型 /// private static ConnectionType connectionType = ConnectionType.SqlServer; public static ConnectionType ConnectionType { get { return connectionType; } set { connectionType = value; } } /// /// 数据连接工厂 /// public static DbProviderFactory dbFactory; /// /// 数据连接 /// public static DbConnection _connection; /// /// 请求超时时间 /// public static int CommandTimeout; /// /// 上一次失败的sql /// public static string LastFailureSql; /// /// 说明:是否已经连接数据库 /// 创建者:朗速科技有限公司 /// 创建日期:2024-3-5 /// 功能关联:公版方法 /// /// public static bool IsConnect() { bool isConnect = _connection.State == ConnectionState.Open; return isConnect; } #region ExecuteAdapter /// /// 说明:获取适配器 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// Type of the command. /// The command text. /// The command parameters. /// SqlDataAdapter. public static DbDataAdapter ExecuteAdapter(CommandType cmdType, string cmdText, params DbParameter[] commandParameters) { DbDataAdapter adapter = dbFactory.CreateDataAdapter(); DbCommand cmd = dbFactory.CreateCommand(); try { PrepareCommand(cmd, _connection, null, cmdType, cmdText, commandParameters); adapter.SelectCommand = cmd; } catch { throw; } return adapter; } /// /// 说明:获取适配器 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// Type of the command. /// The command text. /// SqlDataAdapter. public static DbDataAdapter ExecuteAdapter(CommandType cmdType, string cmdText) { DbDataAdapter adapter = dbFactory.CreateDataAdapter(); DbCommand cmd = dbFactory.CreateCommand(); try { PrepareCommand(cmd, _connection, null, cmdType, cmdText, null); adapter.SelectCommand = cmd; } catch { throw; } return adapter; } #endregion #region ExecuteDataTable /// /// 说明:执行sql返回DataTable /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// The command text. /// DataTable. public static DataTable ExecuteDataTable(string cmdText) { return ExecuteDataSet(cmdText).Tables[0]; } /// /// 说明:执行sql返回DataTable /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// The command text. /// DataTable. public static DataTable ExecuteDataTable(string cmdText, params DbParameter[] commandParameters) { return ExecuteDataSet(CommandType.Text, cmdText, "temp", commandParameters).Tables[0]; } /// /// 说明:执行sql返回指定字段值 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// Name of the field. /// The command text. /// System.Object. public static object ExecuteObject(string fieldName, string cmdText) { object result = null; try { result = ExecuteDataSet(cmdText).Tables[0].Rows[0][fieldName]; } catch (Exception) { } return result; } /// /// 说明:执行sql返回指定字段值 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// Name of the field. /// The command text. /// The command parameters. /// System.Object. public static object ExecuteObject(string fieldName, string cmdText, params DbParameter[] commandParameters) { object result = null; try { result = ExecuteDataSet(CommandType.Text, cmdText, "temp", commandParameters).Tables[0].Rows[0][fieldName]; } catch (Exception) { } return result; } /// /// 说明:执行sql返回指定字段值 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// Name of the field. /// The command text. /// System.Object. public static string ExecuteString(string fieldName, string cmdText) { object obj = ExecuteObject(fieldName, cmdText); return obj == null ? "" : obj.ToString(); } /// /// 说明:执行sql返回指定字段值 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// Name of the field. /// The command text. /// The command parameters. /// System.String. public static string ExecuteString(string fieldName, string cmdText, params DbParameter[] commandParameters) { object obj = ExecuteObject(fieldName, cmdText, commandParameters); return obj == null ? "" : obj.ToString(); } #endregion #region ExecuteDataSet /// /// 说明:查询结果集 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Type of the command. /// The command text. /// Name of the tab. /// The command parameters. /// DataSet. public static DataSet ExecuteDataSet(int cmdType, string cmdText, string tabName, params DbParameter[] commandParameters) { CommandType _cmdType = CommandType.Text; switch (cmdType) { case 4: _cmdType = CommandType.StoredProcedure; break; case 512: _cmdType = CommandType.TableDirect; break; } return ExecuteDataSet(_cmdType, cmdText, tabName, commandParameters); } /// /// 说明:查询结果集 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// 语句类型存储过程或者sql语句 /// 执行内容 /// 表名 /// 参数 /// DataSet. public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText, string tabName, params DbParameter[] commandParameters) { LastFailureSql = string.Empty; DataSet set2; DbCommand cmd = dbFactory.CreateCommand(); try { PrepareCommand(cmd, _connection, null, cmdType, cmdText, commandParameters);//sql参数处理 DbDataAdapter adapter = dbFactory.CreateDataAdapter(); adapter.SelectCommand = cmd;//创建对象,与sql命令对象绑定 DataSet dataSet = new DataSet(); adapter.Fill(dataSet, tabName);//填充数据。第二个参数是数据集中内存表的名字,可以与数据库中的不同 if (dataSet.Tables.Count > 0 && commandParameters != null && connectionType == ConnectionType.DmServer) { DataTable resultTable = dataSet.Tables[dataSet.Tables.Count - 1]; object returnValue = resultTable.Columns.Contains("returnValue") ? resultTable.Rows[0]["returnValue"] : ""; for (int i = 0; i < commandParameters.Length; i++) { DbParameter sqlParameter = commandParameters[i]; if (sqlParameter.Direction == ParameterDirection.ReturnValue) { sqlParameter.Value = returnValue; } if (sqlParameter.Direction == ParameterDirection.Output || sqlParameter.Direction == ParameterDirection.InputOutput) { sqlParameter.Value = resultTable.Rows[0][$"outputValue{i}"]; } } if (returnValue + "" == "-1") LastFailureSql = cmd.CommandText; } //adapter.FillSchema(dataSet, SchemaType.Mapped, tabName); set2 = dataSet; } catch (Exception e) { if (e.Message.StartsWith("在从服务器接收结果时发生传输级错误") || e.Message.StartsWith("在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误") || e.Message.StartsWith("在向服务器发送请求时发生传输级错误")) { throw new Exception("无法连接服务器,请检查网络连接."); } else { throw new Exception(e.Message + " SQL=[ " + cmdText + " ]", e); //throw; } } finally { cmd.Dispose(); } return set2; } /// /// 说明:查询结果集 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// 语句类型存储过程或者sql语句 /// 执行内容 /// 表名 /// DataSet. public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText, string tabName) { return ExecuteDataSet(cmdType, cmdText, tabName, null); } /// /// 说明:查询结果集 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// 执行内容 /// DataSet. public static DataSet ExecuteDataSet(string cmdText) { return ExecuteDataSet(CommandType.Text, cmdText, "temp"); } /// /// 说明:查询结果集 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// Name of the procedure. /// 表名 /// The parameter names. /// The parameter values. /// DataSet. public static DataSet ExecuteDataSet(string procedureName, string tabName, string[] parameterNames, DbParameter[] parameterValues) { LastFailureSql = string.Empty; DataSet set2; try { DbCommand cmd = dbFactory.CreateCommand(); cmd.CommandText = procedureName; cmd.CommandType = CommandType.StoredProcedure; if (parameterNames != null && parameterValues != null) { for (int i = 0; i < parameterNames.Length && i < parameterValues.Length; i++) { DbParameter dbParameter = dbFactory.CreateParameter(); dbParameter.ParameterName = parameterNames[i]; dbParameter.Value = parameterValues[i]; cmd.Parameters.Add(dbParameter); } } DbDataAdapter adapter = dbFactory.CreateDataAdapter(); adapter.SelectCommand = cmd; DataSet dataSet = new DataSet(); adapter.Fill(dataSet, tabName); if (dataSet.Tables.Count > 0 && parameterValues != null && connectionType == ConnectionType.DmServer) { DataTable resultTable = dataSet.Tables[dataSet.Tables.Count - 1]; object returnValue = resultTable.Columns.Contains("returnValue") ? resultTable.Rows[0]["returnValue"] : ""; for (int i = 0; i < parameterValues.Length; i++) { DbParameter sqlParameter = parameterValues[i]; if (sqlParameter.Direction == ParameterDirection.ReturnValue) { sqlParameter.Value = returnValue; } if (sqlParameter.Direction == ParameterDirection.Output || sqlParameter.Direction == ParameterDirection.InputOutput) { sqlParameter.Value = resultTable.Rows[0][$"outputValue{i}"]; } } if (returnValue + "" == "-1") LastFailureSql = cmd.CommandText; } set2 = dataSet; } catch { throw; } return set2; } #endregion #region ExecuteNonQuery /// /// 说明:执行sql语句或者存储过程 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// Type of the command. /// The command text. /// The command parameters. /// System.Int32. public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params DbParameter[] commandParameters) { LastFailureSql = string.Empty; DbCommand cmd = dbFactory.CreateCommand(); int num = 0; try { if (connectionType == ConnectionType.KdbnServer || connectionType == ConnectionType.DmServer) { if (cmdType == CommandType.StoredProcedure) { PrepareCommand(cmd, _connection, null, cmdType, cmdText, commandParameters); DbDataAdapter adapter = dbFactory.CreateDataAdapter(); adapter.SelectCommand = cmd; DataSet dataSet = new DataSet(); adapter.Fill(dataSet); if (dataSet.Tables.Count > 0 && commandParameters != null && connectionType == ConnectionType.DmServer) { DataTable resultTable = dataSet.Tables[dataSet.Tables.Count - 1]; int.TryParse(resultTable.Rows[0]["execcount"] + "", out num); object returnValue = resultTable.Columns.Contains("returnValue") ? resultTable.Rows[0]["returnValue"] : ""; for (int i = 0; i < commandParameters.Length; i++) { DbParameter sqlParameter = commandParameters[i]; if (sqlParameter.Direction == ParameterDirection.ReturnValue) { sqlParameter.Value = returnValue; } if (sqlParameter.Direction == ParameterDirection.Output || sqlParameter.Direction == ParameterDirection.InputOutput) { sqlParameter.Value = resultTable.Rows[0][$"outputValue{i}"]; } } if (returnValue + "" == "-1") LastFailureSql = cmd.CommandText; } } else { PrepareCommand(cmd, _connection, null, cmdType, cmdText, commandParameters); num = cmd.ExecuteNonQuery(); } } else if (connectionType == ConnectionType.SqlServer) { PrepareCommand(cmd, _connection, null, cmdType, cmdText, commandParameters); num = cmd.ExecuteNonQuery(); } cmd.Parameters.Clear(); } catch (Exception e) { if (e.Message.StartsWith("在从服务器接收结果时发生传输级错误") || e.Message.StartsWith("在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误") || e.Message.StartsWith("在向服务器发送请求时发生传输级错误")) { throw new Exception("无法连接服务器,请检查网络连接."); } else { throw new Exception(e.Message + " SQL=[ " + cmdText + " ]", e); //throw; } } finally { cmd.Dispose(); } return num; } /// /// 说明:执行sql语句或者存储过程,带事务 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// /// /// /// /// public static int ExecuteNonQuery(CommandType cmdType, string cmdText, DbTransaction Trans, int a, params DbParameter[] commandParameters) { LastFailureSql = string.Empty; DbCommand cmd = dbFactory.CreateCommand(); int num = 0; try { if (_connection is KdbndpConnection || connectionType == ConnectionType.DmServer) { if (cmdType == CommandType.StoredProcedure) { PrepareCommand(cmd, _connection, Trans, cmdType, cmdText, commandParameters); DbDataAdapter adapter = dbFactory.CreateDataAdapter(); adapter.SelectCommand = cmd; DataSet dataSet = new DataSet(); adapter.Fill(dataSet); if (dataSet.Tables.Count > 0 && commandParameters != null && connectionType == ConnectionType.DmServer) { DataTable resultTable = dataSet.Tables[dataSet.Tables.Count - 1]; int.TryParse(resultTable.Rows[0]["execcount"] + "", out num); object returnValue = resultTable.Columns.Contains("returnValue") ? resultTable.Rows[0]["returnValue"] : ""; for (int i = 0; i < commandParameters.Length; i++) { DbParameter sqlParameter = commandParameters[i]; if (sqlParameter.Direction == ParameterDirection.ReturnValue) { sqlParameter.Value = returnValue; } if (sqlParameter.Direction == ParameterDirection.Output || sqlParameter.Direction == ParameterDirection.InputOutput) { sqlParameter.Value = resultTable.Rows[0][$"outputValue{i}"]; } } if (returnValue + "" == "-1") LastFailureSql = cmd.CommandText; } } else { PrepareCommand(cmd, _connection, Trans, cmdType, cmdText, commandParameters); num = cmd.ExecuteNonQuery(); } } else if (_connection is SqlConnection) { PrepareCommand(cmd, _connection, null, cmdType, cmdText, commandParameters); num = cmd.ExecuteNonQuery(); } cmd.Parameters.Clear(); } catch (Exception e) { if (e.Message.StartsWith("在从服务器接收结果时发生传输级错误") || e.Message.StartsWith("在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误") || e.Message.StartsWith("在向服务器发送请求时发生传输级错误")) { throw new Exception("无法连接服务器,请检查网络连接."); } else { throw new Exception(e.Message + " SQL=[ " + cmdText + " ]", e); //throw; } } finally { cmd.Dispose(); } return num; } /// /// 说明:执行sql语句或者存储过程 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// Type of the command. /// The command text. /// System.Int32. public static int ExecuteNonQuery(CommandType cmdType, string cmdText) { return ExecuteNonQuery(CommandType.Text, cmdText, null); } /// /// 说明:执行sql语句或者存储过程 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// The command text. /// System.Int32. public static int ExecuteNonQuery(string cmdText) { return ExecuteNonQuery(CommandType.Text, cmdText); } #endregion #region ExecuteReader /// /// 说明:执行sql语句或者存储过程并返回结果集(只读、只进) /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// Type of the command. /// The command text. /// The command parameters. /// SqlDataReader. public static DbDataReader ExecuteReader(CommandType cmdType, string cmdText, params DbParameter[] commandParameters) { DbDataReader reader2; DbCommand cmd = dbFactory.CreateCommand(); try { PrepareCommand(cmd, _connection, null, cmdType, cmdText, commandParameters); DbDataReader reader = cmd.ExecuteReader(); cmd.Parameters.Clear(); reader2 = reader; } catch { throw; } return reader2; } #endregion #region ExecuteCommand /// /// 说明:执行sql语句或者存储过程并返回SqlCommand(只读、只进) /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Type of the command. /// The command text. /// The command parameters. /// SqlDataReader. public static DbCommand ExecuteCommand(CommandType cmdType, string cmdText, params DbParameter[] commandParameters) { DbCommand cmd = dbFactory.CreateCommand(); try { PrepareCommand(cmd, _connection, null, cmdType, cmdText, commandParameters); cmd.ExecuteNonQuery(); } catch { throw; } return cmd; } #endregion #region ExecuteScalar /// /// 说明:执行sql语句或者存储过程并返回结果集第一行第一列 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// The connectionection. /// Type of the command. /// The command text. /// The command parameters. /// System.Object. public static object ExecuteScalar(DbConnection Connectionection, CommandType cmdType, string cmdText, params DbParameter[] commandParameters) { DbCommand cmd = dbFactory.CreateCommand(); PrepareCommand(cmd, Connectionection, null, cmdType, cmdText, commandParameters); object obj2 = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return obj2; } /// /// 说明:执行sql语句或者存储过程并返回结果集第一行第一列 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// Type of the command. /// The command text. /// The command parameters. /// System.Object. public static object ExecuteScalar(CommandType cmdType, string cmdText, params DbParameter[] commandParameters) { try { DbCommand cmd = dbFactory.CreateCommand(); PrepareCommand(cmd, _connection, null, cmdType, cmdText, commandParameters); object obj2 = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return obj2; } catch (Exception e) { if (e.Message.StartsWith("在从服务器接收结果时发生传输级错误") || e.Message.StartsWith("在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误") || e.Message.StartsWith("在向服务器发送请求时发生传输级错误")) { throw new Exception("无法连接服务器,请检查网络连接."); } else { throw new Exception(e.Message + " SQL=[ " + cmdText + " ]", e); //throw; } } } /// /// 说明:执行sql语句或者存储过程并返回结果集第一行第一列 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// Type of the command. /// The command text. /// System.Object. public static object ExecuteScalar(CommandType cmdType, string cmdText) { DbCommand cmd = dbFactory.CreateCommand(); PrepareCommand(cmd, _connection, null, cmdType, cmdText, null); object obj2 = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return obj2; } /// /// 说明:执行sql语句或者存储过程并返回结果集第一行第一列 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// The command text. /// System.Object. public static object ExecuteScalar(string cmdText) { DbCommand cmd = dbFactory.CreateCommand(); PrepareCommand(cmd, _connection, null, CommandType.Text, cmdText, null); object obj2 = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return obj2; } /// /// 说明:执行sql语句或者存储过程并返回结果集第一行第一列,带事务 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// /// /// /// /// public static object ExecuteScalar(string cmdText, DbTransaction Trans) { DbCommand cmd = dbFactory.CreateCommand(); PrepareCommand(cmd, _connection, Trans, CommandType.Text, cmdText, null); object obj2 = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return obj2; } #endregion #region PrepareCommand /// /// 说明:sql参数处理 /// 创建者:朗速科技有限公司 /// 创建日期:2023-12-28 /// 功能关联:公版方法 /// /// The command. /// The connection. /// The trans. /// Type of the command. /// The command text. /// The command parms. public static void PrepareCommand(DbCommand cmd, DbConnection Connection, DbTransaction trans, CommandType cmdType, string cmdText, DbParameter[] cmdParms) { try { if (connectionType == ConnectionType.KdbnServer || connectionType == ConnectionType.DmServer) { if (connectionType == ConnectionType.KdbnServer) { if (Connection.State == ConnectionState.Open) { Connection.Close(); } Connection.Open(); } else if (connectionType == ConnectionType.DmServer) { if (Connection.State != ConnectionState.Open) { Connection.Open(); } } cmd.Connection = Connection; cmd.CommandTimeout = CommandTimeout; cmd.CommandText = cmdText; if (trans != null) { cmd.Transaction = trans; } DbParameter[] dbParameters = CastParameterArray(cmdParms); if (cmdType == CommandType.StoredProcedure) { string execSqlParas = ""; string outputParas = ""; StringBuilder stringBuilder = new StringBuilder(); bool hasReturnPara = false; if (cmdParms != null) { if (connectionType == ConnectionType.DmServer) { stringBuilder.AppendLine($"declare"); } for (int i = 0; i < dbParameters.Length; i++) { DbParameter parameter = dbParameters[i]; if (parameter.Direction == ParameterDirection.ReturnValue) { if (connectionType == ConnectionType.KdbnServer) { stringBuilder.AppendLine($"declare @returnValue varchar(4000);"); } else if (connectionType == ConnectionType.DmServer) { stringBuilder.AppendLine($"returnValue varchar(4000);"); } hasReturnPara = true; continue; } string value = (parameter.Value + "").Replace("'", "''"); if (parameter.Direction == ParameterDirection.Output || parameter.Direction == ParameterDirection.InputOutput) { if (connectionType == ConnectionType.KdbnServer) { stringBuilder.AppendLine($"declare @outputValue{i} varchar(4000);"); outputParas += $"@outputValue{i} outputValue{i},"; execSqlParas += $"@outputValue{i} output,"; } else if (connectionType == ConnectionType.DmServer) { stringBuilder.AppendLine($"outputValue{i} varchar(4000);"); outputParas += $"outputValue{i} outputValue{i},"; execSqlParas += $"outputValue{i},"; } } else { execSqlParas += $"'{value}',"; } } } if (connectionType == ConnectionType.KdbnServer) { stringBuilder.AppendLine($"exec @returnValue = {cmdText} {execSqlParas.TrimEnd(',')};"); stringBuilder.AppendLine($"select @@ROWCOUNT execcount,@returnValue returnValue{(outputParas.Length > 0 ? $",{outputParas.TrimEnd(',')}" : "")};"); } else if (connectionType == ConnectionType.DmServer) { stringBuilder.AppendLine($"begin"); stringBuilder.AppendLine($"call {cmdText} ({(hasReturnPara ? "returnValue," : "")}{execSqlParas.TrimEnd(',')});"); stringBuilder.AppendLine($"select SQL%ROWCOUNT execcount{(hasReturnPara ? ",returnValue returnValue" : "")}{(outputParas.Length > 0 ? $",{outputParas.TrimEnd(',')}" : "")};"); stringBuilder.AppendLine($"end"); } cmd.CommandType = CommandType.Text; cmd.CommandText = stringBuilder.ToString(); } else { cmd.CommandText = cmdText; if (connectionType == ConnectionType.DmServer) { cmd.CommandText = Regex.Replace(cmd.CommandText, @"\bdbo\.", "", RegexOptions.IgnoreCase); cmd.CommandText = Regex.Replace(cmd.CommandText, @"(? /// 转换为人大金仓参数集合 /// /// /// public static DbParameter[] CastParameterArray(DbParameter[] commandParameters) { if (commandParameters == null) { return null; } DbParameter[] dbParameters = new DbParameter[commandParameters.Length]; for (int i = 0; i < commandParameters.Length; i++) { DbParameter parameter = commandParameters[i]; dbParameters[i] = CastDbParameter(parameter); } return dbParameters; } /// /// 转换为人大金仓参数 /// /// /// public static DbParameter CastDbParameter(DbParameter parameter) { DbParameter dbParameter = dbFactory.CreateParameter(); if (parameter is SqlParameter sqlParameter) { if (dbParameter is KdbndpParameter kdbndpParameter) { KdbndpDbType kdbndpDbType = KdbndpDbType.Text; switch (sqlParameter.SqlDbType) { case SqlDbType.BigInt: kdbndpDbType = KdbndpDbType.SqlServer_Bigint; break; case SqlDbType.Binary: kdbndpDbType = KdbndpDbType.SqlServer_Binary; break; case SqlDbType.Bit: kdbndpDbType = KdbndpDbType.Bit; break; case SqlDbType.Char: kdbndpDbType = KdbndpDbType.Char; break; case SqlDbType.DateTime: kdbndpDbType = KdbndpDbType.DateTime; break; case SqlDbType.Decimal: kdbndpDbType = KdbndpDbType.SqlServer_Numeric; break; case SqlDbType.Float: kdbndpDbType = KdbndpDbType.SqlServer_Float; break; case SqlDbType.Image: kdbndpDbType = KdbndpDbType.Image; break; case SqlDbType.Int: kdbndpDbType = KdbndpDbType.SqlServer_Int; break; case SqlDbType.Money: kdbndpDbType = KdbndpDbType.SqlServer_Money; break; case SqlDbType.NChar: kdbndpDbType = KdbndpDbType.NChar; break; case SqlDbType.NText: kdbndpDbType = KdbndpDbType.Ntext; break; case SqlDbType.NVarChar: kdbndpDbType = KdbndpDbType.Text; break; case SqlDbType.Real: kdbndpDbType = KdbndpDbType.SqlServer_Real; break; case SqlDbType.SmallDateTime: kdbndpDbType = KdbndpDbType.SmallDateTime; break; case SqlDbType.SmallInt: kdbndpDbType = KdbndpDbType.SqlServer_Smallint; break; case SqlDbType.Text: kdbndpDbType = KdbndpDbType.Text; break; case SqlDbType.Timestamp: kdbndpDbType = KdbndpDbType.Timestamp; break; case SqlDbType.TinyInt: kdbndpDbType = KdbndpDbType.SqlServer_Tinyint; break; case SqlDbType.VarBinary: kdbndpDbType = KdbndpDbType.SqlServer_VarBinary; break; case SqlDbType.VarChar: kdbndpDbType = KdbndpDbType.Varchar; break; case SqlDbType.Xml: kdbndpDbType = KdbndpDbType.Xml; break; case SqlDbType.Date: kdbndpDbType = KdbndpDbType.Date; break; case SqlDbType.Time: kdbndpDbType = KdbndpDbType.Time; break; case SqlDbType.DateTime2: kdbndpDbType = KdbndpDbType.DateTime2; break; } kdbndpParameter.KdbndpDbType = kdbndpDbType; } else if (dbParameter is DmParameter dmParameter) { DmDbType dmDbType = DmDbType.Text; switch (sqlParameter.SqlDbType) { case SqlDbType.BigInt: dmDbType = DmDbType.Int64; break; case SqlDbType.Binary: dmDbType = DmDbType.Binary; break; case SqlDbType.Bit: dmDbType = DmDbType.Bit; break; case SqlDbType.Char: dmDbType = DmDbType.Char; break; case SqlDbType.DateTime: dmDbType = DmDbType.DateTime; break; case SqlDbType.Decimal: dmDbType = DmDbType.Decimal; break; case SqlDbType.Float: dmDbType = DmDbType.Float; break; case SqlDbType.Int: dmDbType = DmDbType.Int32; break; break; case SqlDbType.NText: dmDbType = DmDbType.Text; break; case SqlDbType.NVarChar: dmDbType = DmDbType.Text; break; case SqlDbType.SmallInt: dmDbType = DmDbType.Int16; break; case SqlDbType.Text: dmDbType = DmDbType