当前上下文中不存在名称ConfigurationManager的解决方法
今晚做项目在DBHelper.cs类中的数据库连接要改到web.config里面调用,结果在编译的时候却发现提示错误:
当前上下文中不存在名称"ConfigurationManager"
明明是已经添加了using System.Configuration但还是不行,后来回想了一下,还要再添加什么的,怪我不认真,真是的,原来还要在DAL类库中添加引用,加入里面的System.Configuration
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Collections; using System.Collections.Generic; using System.Data.SqlClient; using System.Runtime.InteropServices; using System.Configuration; namespace DBUtility { public abstract class DbHelperSQL { /// <summary> /// 数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库. /// </summary> public static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; /// <summary> /// 创建 SqlCommand 对象实例(用来返回一个整数值) /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <returns>SqlCommand 对象实例</returns> private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters); command.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)); return command; } /// <summary> /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值) /// </summary> /// <param name="connection">数据库连接</param> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <returns>SqlCommand</returns> private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = new SqlCommand(storedProcName, connection) { CommandType = CommandType.StoredProcedure }; foreach (SqlParameter parameter in parameters) { if (parameter != null) { if (((parameter.Direction == ParameterDirection.InputOutput) || (parameter.Direction == ParameterDirection.Input)) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } command.Parameters.Add(parameter); } } return command; } /// <summary> /// 判断是否存在某表的某个字段 /// </summary> /// <param name="tableName">表名称</param> /// <param name="columnName">列名称</param> /// <returns>是否存在</returns> public static bool ColumnExists(string tableName, string columnName) { object single = GetSingle("select count(1) from syscolumns where [id]=object_id('" + tableName + "') and [name]='" + columnName + "'"); if (single == null) { return false; } return (Convert.ToInt32(single) > 0); } /// <summary> /// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close ) /// </summary> /// <param name="strSQL">查询语句</param> /// <returns>SqlDataReader</returns> public static SqlDataReader ExecuteReader(string strSQL) { SqlDataReader reader2; SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(strSQL, connection); try { connection.Open(); reader2 = command.ExecuteReader(CommandBehavior.CloseConnection); } catch (SqlException exception) { throw exception; } return reader2; } /// <summary> /// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close ) /// </summary> /// <param name="strSQL">查询语句</param> /// <returns>SqlDataReader</returns> public static SqlDataReader ExecuteReader(string SQLString, params SqlParameter[] cmdParms) { SqlDataReader reader2; SqlConnection conn = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, conn, null, SQLString, cmdParms); SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); reader2 = reader; } catch (SqlException exception) { throw exception; } return reader2; } /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString">SQL语句</param> /// <returns>影响的记录数</returns> public static int ExecuteSql(string SQLString) { int num2; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(SQLString, connection); try { connection.Open(); num2 = command.ExecuteNonQuery(); } catch (SqlException exception) { connection.Close(); throw exception; } finally { if (command != null) { command.Dispose(); } } } return num2; } /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString">SQL语句</param> /// <returns>影响的记录数</returns> public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms) { int num2; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); int num = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); num2 = num; } catch (SqlException exception) { throw exception; } finally { if (cmd != null) { cmd.Dispose(); } } } return num2; } /// <summary> /// 执行SQL语句,返回影响的记录数,不会自动关闭连接 /// </summary> /// <param name="SQLString">SQL语句</param> /// <param name="_connection">connection</param> /// <returns>影响的记录数</returns> public static int ExecuteSql(string SQLString, SqlConnection _connection) { int num2; SqlCommand command = new SqlCommand(SQLString, _connection); try { _connection.Open(); num2 = command.ExecuteNonQuery(); } catch (SqlException exception) { throw exception; } finally { if (command != null) { command.Dispose(); } } return num2; } /// <summary> /// 执行带一个存储过程参数的的SQL语句。 /// </summary> /// <param name="SQLString">SQL语句</param> /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param> /// <returns>影响的记录数</returns> public static int ExecuteSql(string SQLString, string content) { int num2; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(SQLString, connection); SqlParameter parameter = new SqlParameter("@content", SqlDbType.NText) { Value = content }; command.Parameters.Add(parameter); try { connection.Open(); num2 = command.ExecuteNonQuery(); } catch (SqlException exception) { throw exception; } finally { command.Dispose(); connection.Close(); } } return num2; } /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString">SQL语句</param> /// <returns>影响的记录数</returns> public static int ExecuteSql(string SQLString, SqlConnection conn, params SqlParameter[] cmdParms) { int num2; SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, conn, null, SQLString, cmdParms); int num = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); num2 = num; } catch (SqlException exception) { throw exception; } finally { if (cmd != null) { cmd.Dispose(); } } return num2; } /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString">SQL语句</param> /// <returns>影响的记录数</returns> public static int ExecuteSql(string SQLString, string _connectionString, params SqlParameter[] cmdParms) { int num2; using (SqlConnection connection = new SqlConnection(_connectionString)) { SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); int num = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); num2 = num; } catch (SqlException exception) { throw exception; } finally { if (cmd != null) { cmd.Dispose(); } } } return num2; } public static int ExecuteSqlByTime(string SQLString, int Times) { int num2; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(SQLString, connection); try { connection.Open(); command.CommandTimeout = Times; num2 = command.ExecuteNonQuery(); } catch (SqlException exception) { connection.Close(); throw exception; } finally { if (command != null) { command.Dispose(); } } } return num2; } /// <summary> /// 执行带一个存储过程参数的的SQL语句。 /// </summary> /// <param name="SQLString">SQL语句</param> /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param> /// <returns>影响的记录数</returns> public static object ExecuteSqlGet(string SQLString, string content) { object obj3; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(SQLString, connection); SqlParameter parameter = new SqlParameter("@content", SqlDbType.NText) { Value = content }; command.Parameters.Add(parameter); try { connection.Open(); object objA = command.ExecuteScalar(); if (object.Equals(objA, null) || object.Equals(objA, DBNull.Value)) { return null; } obj3 = objA; } catch (SqlException exception) { throw exception; } finally { command.Dispose(); connection.Close(); } } return obj3; } /// <summary> /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例) /// </summary> /// <param name="strSQL">SQL语句</param> /// <param name="fs">图像字节,数据库的字段类型为image的情况</param> /// <returns>影响的记录数</returns> public static int ExecuteSqlInsertImg(string strSQL, byte[] fs) { int num2; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(strSQL, connection); SqlParameter parameter = new SqlParameter("@fs", SqlDbType.Image) { Value = fs }; command.Parameters.Add(parameter); try { connection.Open(); num2 = command.ExecuteNonQuery(); } catch (SqlException exception) { throw exception; } finally { command.Dispose(); connection.Close(); } } return num2; } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">多条SQL语句</param> public static int ExecuteSqlTran(List<string> SQLStringList) { int num3; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand { Connection = connection }; SqlTransaction transaction = connection.BeginTransaction(); command.Transaction = transaction; try { int num = 0; for (int i = 0; i < SQLStringList.Count; i++) { string str = SQLStringList[i]; if (str.Trim().Length > 1) { command.CommandText = str; num += command.ExecuteNonQuery(); } } transaction.Commit(); num3 = num; } catch { transaction.Rollback(); num3 = 0; } } return num3; } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param> public static void ExecuteSqlTran(Hashtable SQLStringList) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlTransaction transaction = connection.BeginTransaction()) { SqlCommand cmd = new SqlCommand(); try { foreach (DictionaryEntry entry in SQLStringList) { string cmdText = entry.Key.ToString(); SqlParameter[] cmdParms = (SqlParameter[])entry.Value; PrepareCommand(cmd, connection, transaction, cmdText, cmdParms); cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); } transaction.Commit(); } catch { transaction.Rollback(); throw; } } } } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param> public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); using (SqlTransaction transaction = connection.BeginTransaction()) { SqlCommand cmd = new SqlCommand(); try { int num = 0; foreach (DictionaryEntry entry in SQLStringList) { string cmdText = entry.Key.ToString(); SqlParameter[] cmdParms = (SqlParameter[])entry.Value; foreach (SqlParameter parameter in cmdParms) { if (parameter.Direction == ParameterDirection.InputOutput) { parameter.Value = num; } } PrepareCommand(cmd, connection, transaction, cmdText, cmdParms); cmd.ExecuteNonQuery(); foreach (SqlParameter parameter2 in cmdParms) { if (parameter2.Direction == ParameterDirection.Output) { num = Convert.ToInt32(parameter2.Value); } } cmd.Parameters.Clear(); } transaction.Commit(); } catch { transaction.Rollback(); throw; } } } } public static bool Exists(string strSql) { int num; object single = GetSingle(strSql); if (object.Equals(single, null) || object.Equals(single, DBNull.Value)) { num = 0; } else { num = int.Parse(single.ToString()); } if (num == 0) { return false; } return true; } public static bool Exists(string strSql, params SqlParameter[] cmdParms) { int num; object single = GetSingle(strSql, cmdParms); if (object.Equals(single, null) || object.Equals(single, DBNull.Value)) { num = 0; } else { num = int.Parse(single.ToString()); } if (num == 0) { return false; } return true; } /// <summary> /// 得到指定列的最大值 /// </summary> /// <param name="FieldName">列名</param> /// <param name="TableName">表名</param> /// <returns>最大值</returns> public static int GetMaxID(string FieldName, string TableName) { return GetMaxID(FieldName, TableName, ""); } /// <summary> /// 得到指定条件、指定列的最大值 /// </summary> /// <param name="FieldName">列名</param> /// <param name="TableName">表名</param> /// <param name="strWhere">条件</param> /// <returns></returns> public static int GetMaxID(string FieldName, string TableName, string strWhere) { string sQLString = "select max(" + FieldName + ")+1 from " + TableName; if (!string.IsNullOrEmpty(strWhere)) { sQLString = sQLString + " Where " + strWhere; } object single = GetSingle(sQLString); if (single == null) { return 1; } return int.Parse(single.ToString()); } /// <summary> /// 得到指定条件、指定列的最大值 /// </summary> /// <param name="FieldName">列名</param> /// <param name="TableName">表名</param> /// <param name="strWhere">条件</param> /// <returns></returns> public static long GetMaxIDLong(string FieldName, string TableName, string strWhere) { string sQLString = "select max(" + FieldName + ")+1 from " + TableName; if (!string.IsNullOrEmpty(strWhere)) { sQLString = sQLString + " Where " + strWhere; } object single = GetSingle(sQLString); if (single == null) { return 1L; } return long.Parse(single.ToString()); } /// <summary> /// 执行一条计算查询结果语句,返回查询结果(object)。 /// </summary> /// <param name="SQLString">计算查询结果语句</param> /// <returns>查询结果(object)</returns> public static object GetSingle(string SQLString) { object obj3; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(SQLString, connection); try { connection.Open(); object objA = command.ExecuteScalar(); if (object.Equals(objA, null) || object.Equals(objA, DBNull.Value)) { return null; } obj3 = objA; } catch (SqlException exception) { connection.Close(); throw exception; } finally { if (command != null) { command.Dispose(); } } } return obj3; } /// <summary> /// 执行一条计算查询结果语句,返回查询结果(object)。 /// </summary> /// <param name="SQLString">计算查询结果语句</param> /// <returns>查询结果(object)</returns> public static object GetSingle(string SQLString, params SqlParameter[] cmdParms) { object obj3; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); object objA = cmd.ExecuteScalar(); cmd.Parameters.Clear(); if (object.Equals(objA, null) || object.Equals(objA, DBNull.Value)) { return null; } obj3 = objA; } catch (SqlException exception) { throw exception; } finally { if (cmd != null) { cmd.Dispose(); } } } return obj3; } public static object GetSingle(string SQLString, int Times) { object obj3; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(SQLString, connection); try { connection.Open(); command.CommandTimeout = Times; object objA = command.ExecuteScalar(); if (object.Equals(objA, null) || object.Equals(objA, DBNull.Value)) { return null; } obj3 = objA; } catch (SqlException exception) { connection.Close(); throw exception; } finally { if (command != null) { command.Dispose(); } } } return obj3; } public static object GetSingle(string SQLString, string _connectionString) { object obj3; using (SqlConnection connection = new SqlConnection(_connectionString)) { SqlCommand command = new SqlCommand(SQLString, connection); try { connection.Open(); object objA = command.ExecuteScalar(); if (object.Equals(objA, null) || object.Equals(objA, DBNull.Value)) { return null; } obj3 = objA; } catch (SqlException exception) { connection.Close(); throw exception; } finally { if (command != null) { command.Dispose(); } } } return obj3; } public static object GetSingle(string SQLString, string _connectionString, int Times) { object obj3; using (SqlConnection connection = new SqlConnection(_connectionString)) { SqlCommand command = new SqlCommand(SQLString, connection); try { connection.Open(); command.CommandTimeout = Times; object objA = command.ExecuteScalar(); if (object.Equals(objA, null) || object.Equals(objA, DBNull.Value)) { return null; } obj3 = objA; } catch (SqlException exception) { connection.Close(); throw exception; } finally { if (command != null) { command.Dispose(); } } } return obj3; } public static object GetSingle(string SQLString, string _connectionString, params SqlParameter[] cmdParms) { object obj3; using (SqlConnection connection = new SqlConnection(_connectionString)) { SqlCommand cmd = new SqlCommand(); try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); object objA = cmd.ExecuteScalar(); cmd.Parameters.Clear(); if (object.Equals(objA, null) || object.Equals(objA, DBNull.Value)) { return null; } obj3 = objA; } catch (SqlException exception) { throw exception; } finally { if (cmd != null) { cmd.Dispose(); } } } return obj3; } private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms) { if (conn.State != ConnectionState.Open) { conn.Open(); } cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) { cmd.Transaction = trans; } cmd.CommandType = CommandType.Text; if (cmdParms != null) { foreach (SqlParameter parameter in cmdParms) { if (((parameter.Direction == ParameterDirection.InputOutput) || (parameter.Direction == ParameterDirection.Input)) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } cmd.Parameters.Add(parameter); } } } /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString">查询语句</param> /// <returns>DataSet</returns> public static DataSet Query(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet dataSet = new DataSet(); try { connection.Open(); new SqlDataAdapter(SQLString, connection).Fill(dataSet, "ds"); } catch (SqlException exception) { throw new Exception(exception.Message); } return dataSet; } } /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString">查询语句</param> /// <returns>DataSet</returns> public static DataSet Query(string SQLString, params SqlParameter[] cmdParms) { DataSet set2; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, SQLString, cmdParms); using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { DataSet dataSet = new DataSet(); try { adapter.Fill(dataSet, "ds"); cmd.Parameters.Clear(); } catch (SqlException exception) { throw new Exception(exception.Message); } set2 = dataSet; } } return set2; } public static DataSet Query(string SQLString, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet dataSet = new DataSet(); try { connection.Open(); new SqlDataAdapter(SQLString, connection) { SelectCommand = { CommandTimeout = Times } }.Fill(dataSet, "ds"); } catch (SqlException exception) { throw new Exception(exception.Message); } return dataSet; } } public static DataSet Query(string SQLString, string _connectionString) { using (SqlConnection connection = new SqlConnection(_connectionString)) { DataSet dataSet = new DataSet(); try { connection.Open(); new SqlDataAdapter(SQLString, connection).Fill(dataSet, "ds"); } catch (SqlException exception) { throw new Exception(exception.Message); } return dataSet; } } /// <summary> /// 执行存储过程,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close ) /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <returns>SqlDataReader</returns> public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters) { SqlConnection connection = new SqlConnection(connectionString); connection.Open(); SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters); command.CommandType = CommandType.StoredProcedure; return command.ExecuteReader(CommandBehavior.CloseConnection); } /// <summary> /// 执行存储过程 /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <param name="tableName">DataSet结果中的表名</param> /// <returns>DataSet</returns> public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet dataSet = new DataSet(); connection.Open(); new SqlDataAdapter { SelectCommand = BuildQueryCommand(connection, storedProcName, parameters) }.Fill(dataSet, tableName); connection.Close(); return dataSet; } } /// <summary> /// 执行存储过程,返回影响的行数 /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <param name="rowsAffected">影响的行数</param> /// <returns></returns> public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = BuildIntCommand(connection, storedProcName, parameters); rowsAffected = command.ExecuteNonQuery(); return (int)command.Parameters["ReturnValue"].Value; } } public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet dataSet = new DataSet(); connection.Open(); SqlDataAdapter adapter = new SqlDataAdapter { SelectCommand = BuildQueryCommand(connection, storedProcName, parameters) }; adapter.SelectCommand.CommandTimeout = Times; adapter.Fill(dataSet, tableName); connection.Close(); return dataSet; } } /// <summary> /// Runs the procedure. /// </summary> /// <param name="storedProcName">Name of the stored proc.</param> /// <param name="parameters">The parameters.</param> /// <param name="tableName">Name of the table.</param> /// <param name="connstr">The connstr.</param> /// <returns></returns> public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, string connstr) { using (SqlConnection connection = new SqlConnection(connstr)) { DataSet dataSet = new DataSet(); connection.Open(); new SqlDataAdapter { SelectCommand = BuildQueryCommand(connection, storedProcName, parameters) }.Fill(dataSet, tableName); connection.Close(); return dataSet; } } /// <summary> /// 表是否存在 /// </summary> /// <param name="TableName"></param> /// <returns></returns> public static bool TabExists(string TableName) { int num; object single = GetSingle("select count(*) from sysobjects where id = object_id(N'[" + TableName + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1"); if (object.Equals(single, null) || object.Equals(single, DBNull.Value)) { num = 0; } else { num = int.Parse(single.ToString()); } if (num == 0) { return false; } return true; } } }