37aea5dcd1
SVN-Revision: r1061
900 lines
39 KiB
C#
900 lines
39 KiB
C#
using Kdbndp;
|
|
using KdbndpTypes;
|
|
using System;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace Lskj.Core
|
|
{
|
|
/// <summary>
|
|
/// 数据库连接类型
|
|
/// </summary>
|
|
public enum ConnectionType
|
|
{
|
|
SqlServer = 0,
|
|
KdbnServer = 1,
|
|
DmServer = 2
|
|
}
|
|
public static class SqlHelper
|
|
{
|
|
/// <summary>
|
|
/// 连接类型
|
|
/// </summary>
|
|
private static ConnectionType connectionType = ConnectionType.SqlServer;
|
|
public static ConnectionType ConnectionType
|
|
{
|
|
get { return connectionType; }
|
|
set { connectionType = value; }
|
|
}
|
|
/// <summary>
|
|
/// 数据连接工厂
|
|
/// </summary>
|
|
public static DbProviderFactory dbFactory;
|
|
/// <summary>
|
|
/// 数据连接
|
|
/// </summary>
|
|
public static DbConnection _connection;
|
|
/// <summary>
|
|
/// 请求超时时间
|
|
/// </summary>
|
|
public static int CommandTimeout;
|
|
/// <summary>
|
|
/// <para>说明:是否已经连接数据库</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2024-3-5 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static bool IsConnect()
|
|
{
|
|
bool isConnect = _connection.State == ConnectionState.Open;
|
|
return isConnect;
|
|
}
|
|
#region ExecuteAdapter
|
|
/// <summary>
|
|
/// <para>说明:获取适配器</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdType">Type of the command.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <param name="commandParameters">The command parameters.</param>
|
|
/// <returns>SqlDataAdapter.</returns>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:获取适配器</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdType">Type of the command.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <returns>SqlDataAdapter.</returns>
|
|
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
|
|
/// <summary>
|
|
/// <para>说明:执行sql返回DataTable</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <returns>DataTable.</returns>
|
|
public static DataTable ExecuteDataTable(string cmdText)
|
|
{
|
|
return ExecuteDataSet(cmdText).Tables[0];
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:执行sql返回DataTable</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <returns>DataTable.</returns>
|
|
public static DataTable ExecuteDataTable(string cmdText, params DbParameter[] commandParameters)
|
|
{
|
|
return ExecuteDataSet(CommandType.Text, cmdText, "temp", commandParameters).Tables[0];
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:执行sql返回指定字段值</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="fieldName">Name of the field.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <returns>System.Object.</returns>
|
|
public static object ExecuteObject(string fieldName, string cmdText)
|
|
{
|
|
object result = null;
|
|
try
|
|
{
|
|
result = ExecuteDataSet(cmdText).Tables[0].Rows[0][fieldName];
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:执行sql返回指定字段值</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="fieldName">Name of the field.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <param name="commandParameters">The command parameters.</param>
|
|
/// <returns>System.Object.</returns>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:执行sql返回指定字段值</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="fieldName">Name of the field.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <returns>System.Object.</returns>
|
|
public static string ExecuteString(string fieldName, string cmdText)
|
|
{
|
|
object obj = ExecuteObject(fieldName, cmdText);
|
|
return obj == null ? "" : obj.ToString();
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:执行sql返回指定字段值</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="fieldName">Name of the field.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <param name="commandParameters">The command parameters.</param>
|
|
/// <returns>System.String.</returns>
|
|
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
|
|
/// <summary>
|
|
/// <para>说明:查询结果集</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-07-24 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="cmdType">Type of the command.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <param name="tabName">Name of the tab.</param>
|
|
/// <param name="commandParameters">The command parameters.</param>
|
|
/// <returns>DataSet.</returns>
|
|
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);
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:查询结果集</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-07-24 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="cmdType">语句类型存储过程或者sql语句</param>
|
|
/// <param name="cmdText">执行内容</param>
|
|
/// <param name="tabName">表名</param>
|
|
/// <param name="commandParameters">参数</param>
|
|
/// <returns>DataSet.</returns>
|
|
public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText, string tabName, params DbParameter[] commandParameters)
|
|
{
|
|
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);//填充数据。第二个参数是数据集中内存表的名字,可以与数据库中的不同
|
|
//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;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:查询结果集</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdType">语句类型存储过程或者sql语句</param>
|
|
/// <param name="cmdText">执行内容</param>
|
|
/// <param name="tabName">表名</param>
|
|
/// <returns>DataSet.</returns>
|
|
public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText, string tabName)
|
|
{
|
|
return ExecuteDataSet(cmdType, cmdText, tabName, null);
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:查询结果集</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdText">执行内容</param>
|
|
/// <returns>DataSet.</returns>
|
|
public static DataSet ExecuteDataSet(string cmdText)
|
|
{
|
|
return ExecuteDataSet(CommandType.Text, cmdText, "temp");
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:查询结果集</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="procedureName">Name of the procedure.</param>
|
|
/// <param name="tabName">表名</param>
|
|
/// <param name="parameterNames">The parameter names.</param>
|
|
/// <param name="parameterValues">The parameter values.</param>
|
|
/// <returns>DataSet.</returns>
|
|
public static DataSet ExecuteDataSet(string procedureName, string tabName, string[] parameterNames, DbParameter[] parameterValues)
|
|
{
|
|
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);
|
|
set2 = dataSet;
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
return set2;
|
|
}
|
|
#endregion
|
|
#region ExecuteNonQuery
|
|
/// <summary>
|
|
/// <para>说明:执行sql语句或者存储过程</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdType">Type of the command.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <param name="commandParameters">The command parameters.</param>
|
|
/// <returns>System.Int32.</returns>
|
|
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params DbParameter[] commandParameters)
|
|
{
|
|
DbCommand cmd = dbFactory.CreateCommand();
|
|
int num = 0;
|
|
try
|
|
{
|
|
if (_connection is KdbndpConnection)
|
|
{
|
|
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)
|
|
{
|
|
DataTable resultTable = dataSet.Tables[dataSet.Tables.Count - 1];
|
|
int.TryParse(resultTable.Rows[0]["execcount"] + "", out num);
|
|
string returnValue = resultTable.Rows[0]["returnValue"] + "";
|
|
string outputValue = resultTable.Rows[0]["outputValue"] + "";
|
|
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.Value = outputValue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
PrepareCommand(cmd, _connection, null, 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;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:执行sql语句或者存储过程,带事务</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdType"></param>
|
|
/// <param name="cmdText"></param>
|
|
/// <param name="Trans"></param>
|
|
/// <param name="commandParameters"></param>
|
|
/// <returns></returns>
|
|
public static int ExecuteNonQuery(CommandType cmdType, string cmdText, DbTransaction Trans, int a, params DbParameter[] commandParameters)
|
|
{
|
|
DbCommand cmd = dbFactory.CreateCommand();
|
|
int num = 0;
|
|
try
|
|
{
|
|
if (_connection is KdbndpConnection)
|
|
{
|
|
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)
|
|
{
|
|
DataTable resultTable = dataSet.Tables[dataSet.Tables.Count - 1];
|
|
int.TryParse(resultTable.Rows[0]["execcount"] + "", out num);
|
|
string returnValue = resultTable.Rows[0]["returnValue"] + "";
|
|
string outputValue = resultTable.Rows[0]["outputValue"] + "";
|
|
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.Value = outputValue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:执行sql语句或者存储过程</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdType">Type of the command.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <returns>System.Int32.</returns>
|
|
public static int ExecuteNonQuery(CommandType cmdType, string cmdText)
|
|
{
|
|
return ExecuteNonQuery(CommandType.Text, cmdText, null);
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:执行sql语句或者存储过程</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <returns>System.Int32.</returns>
|
|
public static int ExecuteNonQuery(string cmdText)
|
|
{
|
|
return ExecuteNonQuery(CommandType.Text, cmdText);
|
|
}
|
|
#endregion
|
|
#region ExecuteReader
|
|
/// <summary>
|
|
/// <para>说明:执行sql语句或者存储过程并返回结果集(只读、只进)</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdType">Type of the command.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <param name="commandParameters">The command parameters.</param>
|
|
/// <returns>SqlDataReader.</returns>
|
|
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
|
|
/// <summary>
|
|
/// <para>说明:执行sql语句或者存储过程并返回SqlCommand(只读、只进)</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-07-24 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="cmdType">Type of the command.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <param name="commandParameters">The command parameters.</param>
|
|
/// <returns>SqlDataReader.</returns>
|
|
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
|
|
/// <summary>
|
|
/// <para>说明:执行sql语句或者存储过程并返回结果集第一行第一列</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="Connectionection">The connectionection.</param>
|
|
/// <param name="cmdType">Type of the command.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <param name="commandParameters">The command parameters.</param>
|
|
/// <returns>System.Object.</returns>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:执行sql语句或者存储过程并返回结果集第一行第一列</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdType">Type of the command.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <param name="commandParameters">The command parameters.</param>
|
|
/// <returns>System.Object.</returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:执行sql语句或者存储过程并返回结果集第一行第一列</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdType">Type of the command.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <returns>System.Object.</returns>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:执行sql语句或者存储过程并返回结果集第一行第一列</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <returns>System.Object.</returns>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:执行sql语句或者存储过程并返回结果集第一行第一列,带事务</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmdText"></param>
|
|
/// <param name="Trans"></param>
|
|
/// <param name="a"></param>
|
|
/// <param name="b"></param>
|
|
/// <returns></returns>
|
|
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
|
|
/// <summary>
|
|
/// <para>说明:sql参数处理</para>
|
|
/// <para>创建者:朗速科技有限公司</para>
|
|
/// <para>创建日期:2023-12-28 </para>
|
|
/// <para>功能关联:公版方法</para>
|
|
/// </summary>
|
|
/// <param name="cmd">The command.</param>
|
|
/// <param name="Connection">The connection.</param>
|
|
/// <param name="trans">The trans.</param>
|
|
/// <param name="cmdType">Type of the command.</param>
|
|
/// <param name="cmdText">The command text.</param>
|
|
/// <param name="cmdParms">The command parms.</param>
|
|
private static void PrepareCommand(DbCommand cmd, DbConnection Connection, DbTransaction trans, CommandType cmdType, string cmdText, DbParameter[] cmdParms)
|
|
{
|
|
try
|
|
{
|
|
if (connectionType == ConnectionType.KdbnServer)
|
|
{
|
|
if (Connection.State == ConnectionState.Open)
|
|
{
|
|
Connection.Close();
|
|
}
|
|
Connection.Open();
|
|
cmd.Connection = Connection;
|
|
cmd.CommandTimeout = CommandTimeout;
|
|
cmd.CommandText = cmdText;
|
|
if (trans != null)
|
|
{
|
|
cmd.Transaction = trans;
|
|
}
|
|
DbParameter[] kdbndpParameters = CastKdbndpParameterArray(cmdParms);
|
|
if (cmdType == CommandType.StoredProcedure)
|
|
{
|
|
string execSqlParas = "";
|
|
if (cmdParms != null)
|
|
{
|
|
foreach (KdbndpParameter parameter in kdbndpParameters)
|
|
{
|
|
if (parameter.Direction == ParameterDirection.ReturnValue)
|
|
{
|
|
continue;
|
|
}
|
|
string value = (parameter.Value + "").Replace("'", "''");
|
|
if (parameter.Direction == ParameterDirection.Output)
|
|
{
|
|
execSqlParas += $"@outputValue output,";
|
|
}
|
|
else
|
|
{
|
|
execSqlParas += $"'{value}',";
|
|
}
|
|
}
|
|
}
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
stringBuilder.AppendLine("declare @returnValue varchar(max)");
|
|
stringBuilder.AppendLine("declare @outputValue varchar(max)");
|
|
stringBuilder.AppendLine($"exec @returnValue = {cmdText} {execSqlParas.TrimEnd(',')}");
|
|
stringBuilder.AppendLine("select @@ROWCOUNT execcount,@returnValue returnValue,@outputValue outputValue");
|
|
cmd.CommandType = CommandType.Text;
|
|
cmd.CommandText = stringBuilder.ToString();
|
|
}
|
|
else
|
|
{
|
|
cmd.CommandText = cmdText;
|
|
if (cmdParms != null)
|
|
{
|
|
foreach (KdbndpParameter parameter in kdbndpParameters)
|
|
{
|
|
cmd.Parameters.Add(parameter);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (connectionType == ConnectionType.SqlServer || 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;
|
|
}
|
|
cmd.CommandType = cmdType;
|
|
if (cmdParms != null)
|
|
{
|
|
foreach (SqlParameter parameter in cmdParms)
|
|
{
|
|
cmd.Parameters.Add(parameter);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
throw;
|
|
}
|
|
}
|
|
#endregion
|
|
/// <summary>
|
|
/// 转换为人大金仓参数集合
|
|
/// </summary>
|
|
/// <param name="commandParameters"></param>
|
|
/// <returns></returns>
|
|
public static DbParameter[] CastKdbndpParameterArray(DbParameter[] commandParameters)
|
|
{
|
|
if (commandParameters == null)
|
|
{
|
|
return null;
|
|
}
|
|
DbParameter[] kdbndpParameters = new DbParameter[commandParameters.Length];
|
|
for (int i = 0; i < commandParameters.Length; i++)
|
|
{
|
|
DbParameter sqlParameter = commandParameters[i];
|
|
kdbndpParameters[i] = CastKdbndpParameter(sqlParameter);
|
|
}
|
|
return kdbndpParameters;
|
|
}
|
|
/// <summary>
|
|
/// 转换为人大金仓参数
|
|
/// </summary>
|
|
/// <param name="sqlParameter"></param>
|
|
/// <returns></returns>
|
|
public static DbParameter CastKdbndpParameter(DbParameter parameter)
|
|
{
|
|
KdbndpParameter kdbndpParameter = new KdbndpParameter();
|
|
KdbndpDbType kdbndpDbType = KdbndpDbType.Text;
|
|
if (parameter is SqlParameter sqlParameter)
|
|
{
|
|
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:
|
|
kdbndp |