/****************************** * 说明:数据库操作类 * 创建人:龚宇超 * 创建日期:2017-07-24 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; namespace Lskj.GarbledCodeProcessing { /// /// 数据库操作类 /// public static class SqlHelper { /// /// 数据连接 /// public static SqlConnection _connection; /// /// 请求超时时间 /// public static int CommandTimeout; #region ExecuteAdapter /// /// 说明:获取适配器 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Type of the command. /// The command text. /// The command parameters. /// SqlDataAdapter. public static SqlDataAdapter ExecuteAdapter(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlDataAdapter adapter; SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, _connection, null, cmdType, cmdText, commandParameters); adapter = new SqlDataAdapter(cmd); } catch { throw; } return adapter; } /// /// 说明:获取适配器 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Type of the command. /// The command text. /// SqlDataAdapter. public static SqlDataAdapter ExecuteAdapter(CommandType cmdType, string cmdText) { SqlDataAdapter adapter; SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, _connection, null, cmdType, cmdText, null); adapter = new SqlDataAdapter(cmd); } catch { throw; } return adapter; } #endregion #region ExecuteDataTable /// /// 说明:执行sql返回DataTable /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The command text. /// DataTable. public static DataTable ExecuteDataTable(string cmdText) { return ExecuteDataSet(cmdText).Tables[0]; } /// /// 说明:执行sql返回DataTable /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The command text. /// DataTable. public static DataTable ExecuteDataTable(string cmdText, params SqlParameter[] commandParameters) { return ExecuteDataSet(CommandType.Text, cmdText, "temp", commandParameters).Tables[0]; } /// /// 说明:执行sql返回指定字段值 /// 创建人:龚宇超 /// 创建日期:2017-08-15 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// 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返回指定字段值 /// 创建人:龚宇超 /// 创建日期:2017-11-27 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Name of the field. /// The command text. /// The command parameters. /// System.Object. public static object ExecuteObject(string fieldName, string cmdText, params SqlParameter[] commandParameters) { object result = null; try { result = ExecuteDataSet(CommandType.Text, cmdText, "temp", commandParameters).Tables[0].Rows[0][fieldName]; } catch (Exception) { } return result; } /// /// 说明:执行sql返回指定字段值 /// 创建人:龚宇超 /// 创建日期:2017-08-15 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// 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返回指定字段值 /// 创建人:龚宇超 /// 创建日期:2017-11-27 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Name of the field. /// The command text. /// The command parameters. /// System.String. public static string ExecuteString(string fieldName, string cmdText, params SqlParameter[] 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 SqlParameter[] 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 SqlParameter[] commandParameters) { DataSet set2; SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, _connection, null, cmdType, cmdText, commandParameters); SqlDataAdapter adapter = new SqlDataAdapter(cmd); 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; } } finally { cmd.Dispose(); } return set2; } /// /// 说明:查询结果集 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// 语句类型存储过程或者sql语句 /// 执行内容 /// 表名 /// DataSet. public static DataSet ExecuteDataSet(CommandType cmdType, string cmdText, string tabName) { return ExecuteDataSet(cmdType, cmdText, tabName, null); } /// /// 说明:查询结果集 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// 执行内容 /// DataSet. public static DataSet ExecuteDataSet(string cmdText) { return ExecuteDataSet(CommandType.Text, cmdText, "temp"); } /// /// 说明:查询结果集 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Name of the procedure. /// 表名 /// The parameter names. /// The parameter values. /// DataSet. public static DataSet ExecuteDataSet(string procedureName, string tabName, string[] parameterNames, object[] parameterValues) { DataSet set2; try { SqlCommand cmd = new SqlCommand(procedureName, _connection); cmd.CommandType = CommandType.StoredProcedure; if (parameterNames != null && parameterValues != null) { for (int i = 0; i < parameterNames.Length && i < parameterValues.Length; i++) { cmd.Parameters.AddWithValue(parameterNames[i], parameterValues[i]); } } SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet dataSet = new DataSet(); adapter.Fill(dataSet, tabName); set2 = dataSet; } catch { throw; } return set2; } #endregion #region ExecuteNonQuery /// /// 说明:执行sql语句或者存储过程 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Type of the command. /// The command text. /// The command parameters. /// System.Int32. public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); int num = 0; try { 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; } } finally { cmd.Dispose(); } return num; } /// /// 说明:执行sql语句或者存储过程 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Type of the command. /// The command text. /// System.Int32. public static int ExecuteNonQuery(CommandType cmdType, string cmdText) { return ExecuteNonQuery(CommandType.Text, cmdText, null); } /// /// 说明:执行sql语句或者存储过程 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The command text. /// System.Int32. public static int ExecuteNonQuery(string cmdText) { return ExecuteNonQuery(CommandType.Text, cmdText); } #endregion #region ExecuteReader /// /// 说明:执行sql语句或者存储过程并返回结果集(只读、只进) /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Type of the command. /// The command text. /// The command parameters. /// SqlDataReader. public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlDataReader reader2; SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, _connection, null, cmdType, cmdText, commandParameters); SqlDataReader reader = cmd.ExecuteReader(); cmd.Parameters.Clear(); reader2 = reader; } catch { throw; } return reader2; } #endregion #region ExecuteScalar /// /// 说明:执行sql语句或者存储过程并返回结果集第一行第一列 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The connectionection. /// Type of the command. /// The command text. /// The command parameters. /// System.Object. public static object ExecuteScalar(SqlConnection Connectionection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, Connectionection, null, cmdType, cmdText, commandParameters); object obj2 = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return obj2; } /// /// 说明:执行sql语句或者存储过程并返回结果集第一行第一列 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Type of the command. /// The command text. /// The command parameters. /// System.Object. public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); { PrepareCommand(cmd, _connection, null, cmdType, cmdText, commandParameters); object obj2 = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return obj2; } } /// /// 说明:执行sql语句或者存储过程并返回结果集第一行第一列 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Type of the command. /// The command text. /// System.Object. public static object ExecuteScalar(CommandType cmdType, string cmdText) { SqlCommand cmd = new SqlCommand(); { PrepareCommand(cmd, _connection, null, cmdType, cmdText, null); object obj2 = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return obj2; } } /// /// 说明:执行sql语句或者存储过程并返回结果集第一行第一列 /// 创建人:龚宇超 /// 创建日期:2017-08-21 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The command text. /// System.Object. public static object ExecuteScalar(string cmdText) { SqlCommand cmd = new SqlCommand(); { PrepareCommand(cmd, _connection, null, CommandType.Text, cmdText, null); object obj2 = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return obj2; } } #endregion #region PrepareCommand /// /// 说明: sql参数处理 /// 创建人:龚宇超 /// 创建日期:2017-07-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The command. /// The connection. /// The trans. /// Type of the command. /// The command text. /// The command parms. private static void PrepareCommand(SqlCommand cmd, SqlConnection Connection, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) { 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); } } } #endregion } }