重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
成都创新互联专注于汇川网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供汇川营销型网站建设,汇川网站制作、汇川网页设计、汇川网站官网定制、成都小程序开发服务,打造汇川网络公司原创品牌,更为您提供汇川网站排名全网营销落地服务。
lyqConnectionString 要换
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Configuration;
- using System.Data.Common;
- using System.Data;
- using System.Data.SqlClient;
- namespace LyqDAL
- {
- ///
- /// 自定义访问通用类
- ///
- public class DBHelper
- {
- private static readonly string connectionString = ConfigurationManager.ConnectionStrings["lyqConnectionString"].ConnectionString.ToString();
- private static readonly string providerName = "System.Data.SqlClient";
- private static SqlConnection connection = new SqlConnection(connectionString);
- ///
- /// GetConnection 用于获取连接数据库的 connection 对象
- ///
- ///
- private static DbConnection GetConnection()
- {
- DbProviderFactory _factory = DbProviderFactories.GetFactory(providerName);
- DbConnection connection = _factory.CreateConnection();
- connection.ConnectionString = connectionString;
- return connection;
- }
- ///
- /// GetCommand 获取命令参数 command 对象
- ///
- ///
- ///
- ///
- ///
- private static DbCommand GetCommand(string commandText, CommandType commandType, DbConnection connection)
- {
- DbCommand command = connection.CreateCommand();
- command.CommandText = commandText;
- command.CommandType = commandType;
- return command;
- }
- ///
- /// 读取数据
- ///
- /// sql语句
- /// 参数列表
- ///
DataTable - public static DataTable GetDataTable(string sql, params SqlParameter[] param)
- {
- DataTable dt = new DataTable();
- using (SqlConnection conn = new SqlConnection(connectionString))
- {
- SqlDataAdapter da = new SqlDataAdapter(sql, conn);
- if (param != null)
- da.SelectCommand.Parameters.AddRange(param);
- da.Fill(dt);
- }
- return dt;
- }
- ///
- /// GetCommand 方法重载
- ///
- /// sql语句
- ///
- ///
- private static DbCommand GetCommand(string commandText, DbConnection connection)
- {
- DbCommand command = connection.CreateCommand();
- command.CommandText = commandText;
- command.CommandType = CommandType.Text;
- return command;
- }
- public DbCommand GetSqlStringCommond(string sqlQuery)
- {
- DbCommand dbCommand = connection.CreateCommand();
- dbCommand.CommandText = sqlQuery;
- dbCommand.CommandType = CommandType.Text;
- return dbCommand;
- }
- ///
- /// GetParameter 用于为命令设置参数
- ///
- ///
- ///
- ///
- ///
- private static DbParameter GetParameter(string paramName, object paramValue, DbCommand command)
- {
- DbParameter parameter = command.CreateParameter();
- parameter.ParameterName = paramName;
- parameter.Value = paramValue;
- return parameter;
- }
- ///
- /// 执行无参的存储过程
- ///
- /// 存储过程名
- ///
- public static int ExecuteNonQueryProc(string sqlCommand)
- {
- int result = 0;
- using (DbConnection connection = GetConnection())
- {
- DbCommand command = GetCommand(sqlCommand, CommandType.StoredProcedure, connection);
- connection.Open();
- result = command.ExecuteNonQuery();
- command.Parameters.Clear();
- }
- return result;
- }
- ///
- /// 执行无返回值有参数的存储过程
- ///
- /// 存储过程名
- /// 参数
- ///
- public static int ExecuteNonQueryProc(string sqlCommand, Dictionary
parameters) - {
- int result = 0;
- using (DbConnection connection = GetConnection())
- {
- DbCommand command = GetCommand(sqlCommand, CommandType.StoredProcedure, connection);
- foreach (KeyValuePair
p in parameters) - {
- command.Parameters.Add(GetParameter(p.Key, p.Value, command));
- }
- connection.Open();
- result = command.ExecuteNonQuery();
- command.Parameters.Clear();
- }
- return result;
- }
- ///
- /// 执行无返回值的sql语句
- ///
- ///
- ///
- public static int ExecuteNonQuery(string sqlCommand)
- {
- int result = 0;
- using (DbConnection connection = GetConnection())
- {
- DbCommand command = GetCommand(sqlCommand, CommandType.Text, connection);
- connection.Open();
- result = command.ExecuteNonQuery();
- command.Parameters.Clear();
- return result;
- }
- }
- ///
- /// 执行有参数的sql语句
- ///
- ///
- ///
- ///
- public static int ExecuteNonQuery(string sqlCommand, Dictionary
para) - {
- int result = 0;
- using (DbConnection connection = GetConnection())
- {
- DbCommand command = GetCommand(sqlCommand, CommandType.Text, connection);
- foreach (KeyValuePair
p in para) - {
- command.Parameters.Add(GetParameter(p.Key, p.Value, command));
- }
- connection.Open();
- result = command.ExecuteNonQuery();
- command.Parameters.Clear();
- return result;
- }
- }
- ///
- /// 执行有返回值无参数的存储过程
- ///
- ///
- ///
- public static object ExecuteScalarProc(string cmdText)
- {
- using (DbConnection connection = GetConnection())
- {
- DbCommand command = GetCommand(cmdText, CommandType.StoredProcedure, connection);
- connection.Open();
- object val = command.ExecuteScalar();
- command.Parameters.Clear();
- return val;
- }
- }
- ///
- /// 执行有返回值的有参数的存储过程
- ///
- /// 存储过程名
- /// 参数
- ///
- public static object ExecuteScalarProc(string cmdText, Dictionary
para) - {
- using (DbConnection connection = GetConnection())
- {
- DbCommand command = GetCommand(cmdText, CommandType.StoredProcedure, connection);
- foreach (KeyValuePair
p in para) - {
- command.Parameters.Add(GetParameter(p.Key, p.Value, command));
- }
- connection.Open();
- object val = command.ExecuteScalar();
- command.Parameters.Clear();
- return val;
- }
- }
- ///
- /// 执行有返回值的sql语句
- ///
- ///
- ///
- public static object ExecuteScalar(string cmdText)
- {
- using (DbConnection connection = GetConnection())
- {
- DbCommand command = GetCommand(cmdText, CommandType.Text, connection);
- connection.Open();
- object val = command.ExecuteScalar();
- command.Parameters.Clear();
- return val;
- }
- }
- ///
- /// 执行有返回值有参数的sql语句
- ///
- ///
- ///
- ///
- public static object ExecuteScalar(string cmdText, Dictionary
para) - {
- using (DbConnection connection = GetConnection())
- {
- DbCommand command = GetCommand(cmdText, CommandType.Text, connection);
- foreach (KeyValuePair
p in para) - {
- command.Parameters.Add(GetParameter(p.Key, p.Value, command));
- }
- connection.Open();
- object val = command.ExecuteScalar();
- command.Parameters.Clear();
- return val;
- }
- }
- ///
- /// 执行无参数的存储过程,返回DbDataReader对象
- ///
- /// 存储过程名
- ///
- public static DbDataReader GetReaderProc(string sqlCommand)
- {
- try
- {
- DbConnection connection = GetConnection();
- DbCommand command = GetCommand(sqlCommand, CommandType.StoredProcedure, connection);
- connection.Open();
- DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
- return reader;
- }
- catch (Exception ex)
- {
- Console.Write("" + ex.Message);
- return null;
- }
- }
- ///
- /// 执行有参数的存储过程,返回DbDataReader对象
- ///
- ///
- ///
- ///
- public static DbDataReader GetReaderProc(string sqlCommand, Dictionary
parameters) - {
- try
- {
- DbConnection connection = GetConnection();
- DbCommand command = GetCommand(sqlCommand, CommandType.StoredProcedure, connection);
- foreach (KeyValuePair
p in parameters) - {
- command.Parameters.Add(GetParameter(p.Key, p.Value, command));
- }
- connection.Open();
- DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
- command.Parameters.Clear();
- return reader;
- }
- catch
- {
- return null;
- }
- }
- #region
- ///
- /// 执行无参数的sql语句,返回DbDataReader对象
- ///
- ///
- ///
- public static DbDataReader GetReader(string sqlCommand)
- {
- try
- {
- DbConnection connection = GetConnection();
- DbCommand command = GetCommand(sqlCommand, CommandType.Text, connection);
- connection.Open();
- DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
- command.Parameters.Clear();
- return reader;
- }
- catch (Exception ex)
- {
- Console.Write("" + ex.Message);
- return null;
- }
- }
- #endregion
- ///
- /// 执行有参数的sql语句,返回DbDataReader对象
- ///
- ///
- ///
- ///
- public static DbDataReader GetReader(string sqlCommand, Dictionary
parameters) - {
- try
- {
- DbConnection connection = GetConnection();
- DbCommand command = GetCommand(sqlCommand, CommandType.Text, connection);
- foreach (KeyValuePair
p in parameters) - {
- command.Parameters.Add(GetParameter(p.Key, p.Value, command));
- }
- connection.Open();
- DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
- command.Parameters.Clear();
- return reader;
- }
- catch (Exception ex)
- {
- Console.Write("" + ex.Message);
- return null;
- }
- }
- ///
- /// 返回DataTable对象
- ///
- ///
- ///
- public static DataTable GetDataSet(string safeSql)
- {
- /*DbProviderFactory _factory = DbProviderFactories.GetFactory(providerName);
- DbConnection connection = GetConnection();
- connection.Open();
- DbDataAdapter da = _factory.CreateDataAdapter();
- da.SelectCommand = connection.CreateCommand();
- da.SelectCommand.CommandText = safeSql;
- DataTable dt = new DataTable();
- da.Fill(dt);
- return dt;*/
- using (DbConnection connection = GetConnection())
- {
- DbProviderFactory _factory = DbProviderFactories.GetFactory(providerName);
- DbCommand command = GetCommand(safeSql, CommandType.Text, connection);
- connection.Open();
- DbDataAdapter da = _factory.CreateDataAdapter();
- da.SelectCommand = command;
- DataTable datatable = new DataTable();
- da.Fill(datatable);
- return datatable;
- }
- }
- }
- }