Web.config文件以获取连接 [英] Web.config file for get connection
本文介绍了Web.config文件以获取连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ConnectionString属性尚未初始化-这是一个例外.
我的代码:
The ConnectionString property has not been initialized- This is an exception.
My code:
<configuration>
<connectionStrings>
<add name="SqlConnectionString" connectionString="server=TRES-LED-2\SQLEXPRESS;persist security info=true;database =dbSCBL;user=sa;password=database;connect Timeout=5"/>
</connectionStrings>
我的C#代码是:
My C# code is :
SqlDBAccess db = new SqlDBAccess();
protected void Button1_Click(object sender, EventArgs e)
{
try
{
db.command.Parameters.Add("@id", SqlDbType.Int).Value = int.Parse(txtid.Text.ToString());
db.command.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text.ToString();
db.Adapter("InsertTest", true);
}
catch (Exception ex)
{
lblConfirm.Visible = true;
lblConfirm.Text = ex.Message.ToString();
}
}
我的SqlDBAccess类是:
My SqlDBAccess class is :
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
namespace DatabaseOperation
{
public class SqlDBAccess
{
#region Variables
private string _connectionString;
private string _databaseName;
private string _serverName;
private string _userName;
private string _password;
public SqlConnection connection = new SqlConnection();
public SqlCommand command = new SqlCommand();
private SqlDataAdapter adapter = new SqlDataAdapter();
private SqlDataReader reader;
public DataSet dataSet = new DataSet();
private DataTable dataTable = new DataTable();
private SqlCommandBuilder commandBld = new SqlCommandBuilder();
public SqlTransaction transaction = null;
private Object obj;
#endregion
#region Properties
public string ConnectionString
{
set
{
_connectionString = value;
}
get
{
return _connectionString;
}
}
public string DatabaseName
{
set
{
_databaseName = value;
connection.ChangeDatabase(_databaseName);
}
get
{
return _databaseName;
}
}
public string ServerName
{
set
{
_serverName = value;
}
get
{
return _serverName;
}
}
public string UserName
{
set
{
_userName = value;
}
get
{
return _userName;
}
}
public string Password
{
set
{
_password = value;
}
get
{
return _password;
}
}
#endregion
#region Constructor
public SqlDBAccess()
{
_connectionString = ConfigurationSettings.AppSettings["Server"]; //"server=(local);database = db_IFRMS;persist Security Info=False;user=sa;password=@pwd99$02!;connect Timeout=5";
//DecriptString();
}
public SqlDBAccess(string conStr)
{
_connectionString = conStr;
//DecriptString();
}
#endregion
#region Security Methods
private void DecriptString()
{
string[] parts = _connectionString.Split(';');
string[] words = parts[0].Split('=');
_serverName = words[1].Trim();
words = parts[1].Split('=');
_databaseName = words[1].Trim();
words = parts[3].Split('=');
_userName = words[1].Trim();
words = parts[4].Split('=');
_password = words[1].Trim();
}
#endregion
#region Connection Method
public void OpenConnection()
{
try
{
if (!(connection.State.ToString() == "Open"))
{
connection.ConnectionString = _connectionString;
connection.Open();
}
}
catch (SqlException ex)
{
Log("Error in open connection", ex);
throw ex;
}
catch (Exception ex)
{
Log("Error in open connection", ex);
throw ex;
}
}
public void CloseConnection()
{
try
{
if (connection.State.ToString() == "Open")
connection.Close();
}
catch (SqlException ex)
{
Log("Error in close connection", ex);
throw new ApplicationException("Error in close connection", ex);
}
catch (Exception ex)
{
Log("Error in close connection", ex);
throw new ApplicationException("Error in close connection", ex);
}
}
#endregion
#region ExecuteNonQuery
public int ExecuteNonQuery(string sqlStmt)
{
OpenConnection();
int rowCount = 0;
try
{
transaction = connection.BeginTransaction();
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
command.Transaction = transaction;
rowCount = command.ExecuteNonQuery();
transaction.Commit();
}
catch (SqlException ex)
{
if (transaction != null)
transaction.Rollback();
Log("Error in ExecuteQuery", ex);
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
if (transaction != null)
transaction.Rollback();
Log("Error in ExecuteQuery", ex);
throw ex;
}
finally
{
CloseConnection();
}
return rowCount;
}
public int ExecuteNonQuery(string sqlStmt, bool IsStoredProcedure)
{
OpenConnection();
int rowCount = 0;
try
{
transaction = connection.BeginTransaction();
command.CommandText = sqlStmt;
command.Connection = connection;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
command.Transaction = transaction;
rowCount = command.ExecuteNonQuery();
transaction.Commit();
}
catch (SqlException ex)
{
if (transaction != null)
transaction.Rollback();
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
command.Parameters.Clear();
}
return rowCount;
}
public int ExecuteMulNonQuery(string sqlStmt)
{
int rowCount = 0;
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
rowCount = command.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
return rowCount;
}
#endregion
#region Execute Reader Methods
public SqlDataReader ExecuteReader(string sqlStmt)
{
try
{
OpenConnection();
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
reader = command.ExecuteReader();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
return reader;
}
public SqlDataReader ExecuteReader(string sqlStmt, bool IsStoredProcedure)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
reader = command.ExecuteReader();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
return reader;
}
public Object ExecuteScalar(string sqlStmt)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
obj = command.ExecuteScalar();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
}
return obj;
}
#endregion Reader Methods
#region Adapter Methods
public DataTable Adapter(string sqlStmt)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
if (dataTable != null)
dataTable.Clear();
adapter.Fill(dataTable);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
}
return dataTable;
}
public DataTable Adapter(string sqlStmt, bool IsStoredProcedure)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
if (dataTable != null)
dataTable.Clear();
adapter.Fill(dataTable);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
command.Parameters.Clear();
}
return dataTable;
}
public void Adapter(string sqlStmt, string tableName, bool IsStoredProcedure)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
if (dataSet.Tables[tableName] != null)
dataSet.Tables[tableName].Clear();
adapter.Fill(dataSet, tableName);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
command.Parameters.Clear();
}
}
public void Adapter(string sqlStmt, string tableName)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
if (dataSet.Tables[tableName] != null)
dataSet.Tables[tableName].Clear();
adapter.Fill(dataSet, tableName);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
}
}
public void Adapter(string sqlStmt, DataTable table)
{
try
{
OpenConnection();
command.CommandText = sqlStmt;
command.Connection = connection;
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
adapter.Fill(table);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
}
}
public void Adapter(string sqlStmt, DataTable table, bool IsStoredProcedure)
{
OpenConnection();
try
{
command.CommandText = sqlStmt;
command.Connection = connection;
adapter.SelectCommand = command;
if (IsStoredProcedure)
command.CommandType = CommandType.StoredProcedure;
else
command.CommandType = CommandType.Text;
adapter.Fill(table);
commandBld.DataAdapter = adapter;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
command.Parameters.Clear();
}
}
public void DataSetToDB(DataTable table)
{
OpenConnection();
try
{
adapter.Update(table);
dataSet.AcceptChanges();
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
CloseConnection();
}
}
#endregion Adapter Methods
#region Error Log Methods
public static void Log(string Message)
{
Log(Message, null);
}
public static void Log(string Message, Exception Ex)
{
string fileName = Environment.CurrentDirectory + "\\" + "Error.log";
using (StreamWriter logFile = new StreamWriter(fileName, true))
{
logFile.WriteLine("{0}: {1}", DateTime.Now, Message);
if (Ex != null)
logFile.WriteLine(Ex.ToString());
logFile.Close();
}
}
#endregion Error Log Methods
}
}
再请解释一下连接哪里错了?
Than please explain me where is the wrong to get the connection ?
推荐答案
02!; connect Timeout = 5; // DecriptString(); } 公共 SqlDBAccess(字符串 conStr) { _connectionString = conStr; // DecriptString(); } #endregion #region安全方法 私有 无效 DecriptString() { 字符串 []部件= _connectionString.Split(' ;' ); 字符串 []个单词=零件[ 0 ].Split( ' ='); _serverName = words [ 1 ].Trim(); words = parts [ 1 ].Split(' =' ); _databaseName = words [ 1 ].Trim(); words = parts [ 3 ].Split(' =' ); _userName = words [ 1 ].Trim(); words = parts [ 4 ].Split(' =' ); _password =单词[ 1 ].Trim(); } #endregion #region连接方法 公共 void OpenConnection() { 尝试 { 如果(!(connection.State.ToString()== " )) { connection.ConnectionString = _connectionString; connection.Open(); } } 捕获(SqlException ex) { Log(" ,例如); 扔 ex; } 捕获(例外) { Log(" ,例如); 扔 ex; } } 公共 无效 CloseConnection() { 尝试 { 如果(connection.State.ToString()== " ) connection.Close(); } 捕获(SqlException ex) { Log(" ,例如); 抛出 新 ApplicationException(" 连接紧密时出错",例如); } 捕获(例外) { Log(" ,例如); 抛出 新 ApplicationException(" 连接紧密时出错",例如); } } #endregion #region ExecuteNonQuery 公共 int ExecuteNonQuery(字符串 sqlStmt) { OpenConnection(); int rowCount = 0 ; 尝试 { 交易= connection.BeginTransaction(); command.CommandText = sqlStmt; command.Connection =连接; command.CommandType = CommandType.Text; command.Transaction =交易; rowCount = command.ExecuteNonQuery(); transaction.Commit(); } 捕获(SqlException ex) { 如果(交易!= 空) transaction.Rollback(); Log(" ,例如); 抛出 新 ApplicationException(ex.Message); } 捕获(例外) { 如果(交易!= 空) transaction.Rollback(); Log(" ,例如); 扔 ex; } 最终 { CloseConnection(); } 返回 rowCount; } 公共 int ExecuteNonQuery(字符串 sqlStmt,布尔 IsStoredProcedure) { OpenConnection(); int rowCount = 0 ; 尝试 { 交易= connection.BeginTransaction(); command.CommandText = sqlStmt; command.Connection =连接; 如果(IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; 其他 command.CommandType = CommandType.Text; command.Transaction =交易; rowCount = command.ExecuteNonQuery(); transaction.Commit(); } 捕获(SqlException ex) { 如果(交易!= 空) transaction.Rollback(); 抛出 新 ApplicationException(ex.Message); } 最终 { CloseConnection(); command.Parameters.Clear(); } 返回 rowCount; } 公共 int ExecuteMulNonQuery(字符串 sqlStmt) { int rowCount = 0 ; 尝试 { command.CommandText = sqlStmt; command.Connection =连接; command.CommandType = CommandType.Text; rowCount = command.ExecuteNonQuery(); } 捕获(SqlException ex) { 抛出 新 ApplicationException(ex.Message); } 返回 rowCount; } #endregion #region执行读取器方法 公共 SqlDataReader ExecuteReader(字符串 sqlStmt) { 尝试 { OpenConnection(); command.CommandText = sqlStmt; command.Connection =连接; command.CommandType = CommandType.Text; reader = command.ExecuteReader(); } 捕获(SqlException ex) { 抛出 新 ApplicationException(ex.Message); } 捕获(例外) { 抛出 新 ApplicationException(ex.Message); } 返回阅读器; } 公共 SqlDataReader ExecuteReader(字符串 sqlStmt,布尔 IsStoredProcedure) { OpenConnection(); 尝试 { command.CommandText = sqlStmt; command.Connection =连接; 如果(IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; 其他 command.CommandType = CommandType.Text; reader = command.ExecuteReader(); } 捕获(SqlException ex) { 抛出 新 ApplicationException(ex.Message); } 捕获(例外) { 抛出 新 ApplicationException(ex.Message); } 返回阅读器; } 公共 对象 ExecuteScalar(字符串 sqlStmt) { OpenConnection(); 尝试 { command.CommandText = sqlStmt; command.Connection =连接; command.CommandType = CommandType.Text; obj = command.ExecuteScalar(); } 捕获(SqlException ex) { 抛出 新 ApplicationException(ex.Message); } 最终 { CloseConnection(); } 返回 obj; } #endregion阅读器方法 #region适配器方法 公共 DataTable适配器(字符串 sqlStmt) { OpenConnection(); 尝试 { command.CommandText = sqlStmt; command.Connection =连接; command.CommandType = CommandType.Text; adapter.SelectCommand =命令; 如果(数据表!= 空) dataTable.Clear(); adapter.Fill(dataTable); commandBld.DataAdapter =适配器; } 捕获(SqlException ex) { 抛出 新 ApplicationException(ex.Message); } 最终 { CloseConnection(); } 返回 dataTable; } 公共 DataTable适配器(字符串 sqlStmt,布尔 IsStoredProcedure) { OpenConnection(); 尝试 { command.CommandText = sqlStmt; command.Connection =连接; 如果(IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; 其他 command.CommandType = CommandType.Text; adapter.SelectCommand =命令; 如果(数据表!= 空) dataTable.Clear(); adapter.Fill(dataTable); commandBld.DataAdapter =适配器; } 捕获(SqlException ex) { 抛出 新 ApplicationException(ex.Message); } 最终 { CloseConnection(); command.Parameters.Clear(); } 返回 dataTable; } 公共 无效适配器(字符串 sqlStmt, string tableName, bool IsStoredProcedure) { OpenConnection(); 尝试 { command.CommandText = sqlStmt; command.Connection =连接; 如果(IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; 其他 command.CommandType = CommandType.Text; adapter.SelectCommand =命令; 如果(dataSet.Tables [tableName]!= 空) dataSet.Tables [tableName] .Clear(); adapter.Fill(dataSet,tableName); commandBld.DataAdapter =适配器; } 捕获(SqlException ex) { 抛出 新 ApplicationException(ex.Message); } 最终 { CloseConnection(); command.Parameters.Clear(); } } 公共 无效适配器(字符串 sqlStmt,字符串 tableName) { OpenConnection(); 尝试 { command.CommandText = sqlStmt; command.Connection =连接; command.CommandType = CommandType.Text; adapter.SelectCommand =命令; 如果(dataSet.Tables [tableName]!= 空) dataSet.Tables [tableName] .Clear(); adapter.Fill(dataSet,tableName); commandBld.DataAdapter =适配器; } 捕获(SqlException ex) { 抛出 新 ApplicationException(ex.Message); } 最终 { CloseConnection(); } } 公共 无效适配器(字符串 sqlStmt,DataTable桌子) { 尝试 { OpenConnection(); command.CommandText = sqlStmt; command.Connection =连接; command.CommandType = CommandType.Text; adapter.SelectCommand =命令; adapter.Fill(table); commandBld.DataAdapter =适配器; } 捕获(SqlException ex) { 抛出 新 ApplicationException(ex.Message); } 捕获(例外) { 抛出 新 ApplicationException(ex.Message); } 最终 { CloseConnection(); } } 公共 无效适配器(字符串 sqlStmt,DataTable表,布尔 IsStoredProcedure) { OpenConnection(); 尝试 { command.CommandText = sqlStmt; command.Connection =连接; adapter.SelectCommand =命令; 如果(IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; 其他 command.CommandType = CommandType.Text; adapter.Fill(table); commandBld.DataAdapter =适配器; } 捕获(SqlException ex) { 抛出 新 ApplicationException(ex.Message); } 捕获(例外) { 抛出 新 ApplicationException(ex.Message); } 最终 { CloseConnection(); command.Parameters.Clear(); } } 公共 无效 DataSetToDB(DataTable表) { OpenConnection(); 尝试 { adapter.Update(表); dataSet.AcceptChanges(); } 捕获(SqlException ex) { 抛出 新 ApplicationException(ex.Message); } 最终 { CloseConnection(); } } #endregion适配器方法 #region错误日志方法 公共 静态 无效日志(字符串消息) { 日志(消息,空); } 公共 静态 void Log(字符串消息,例如,例外) { 字符串 fileName = Environment.CurrentDirectory + " + " ; 使用(StreamWriter logFile = 新建 StreamWriter(文件名, true )) { logFile.WriteLine(" ,DateTime.Now,Message); 如果(例如,!= 空) logFile.WriteLine(Ex.ToString()); logFile.Close(); } } #endregion错误日志方法 } }
02!;connect Timeout=5"; //DecriptString(); } public SqlDBAccess(string conStr) { _connectionString = conStr; //DecriptString(); } #endregion #region Security Methods private void DecriptString() { string[] parts = _connectionString.Split(';'); string[] words = parts[0].Split('='); _serverName = words[1].Trim(); words = parts[1].Split('='); _databaseName = words[1].Trim(); words = parts[3].Split('='); _userName = words[1].Trim(); words = parts[4].Split('='); _password = words[1].Trim(); } #endregion #region Connection Method public void OpenConnection() { try { if (!(connection.State.ToString() == "Open")) { connection.ConnectionString = _connectionString; connection.Open(); } } catch (SqlException ex) { Log("Error in open connection", ex); throw ex; } catch (Exception ex) { Log("Error in open connection", ex); throw ex; } } public void CloseConnection() { try { if (connection.State.ToString() == "Open") connection.Close(); } catch (SqlException ex) { Log("Error in close connection", ex); throw new ApplicationException("Error in close connection", ex); } catch (Exception ex) { Log("Error in close connection", ex); throw new ApplicationException("Error in close connection", ex); } } #endregion #region ExecuteNonQuery public int ExecuteNonQuery(string sqlStmt) { OpenConnection(); int rowCount = 0; try { transaction = connection.BeginTransaction(); command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; command.Transaction = transaction; rowCount = command.ExecuteNonQuery(); transaction.Commit(); } catch (SqlException ex) { if (transaction != null) transaction.Rollback(); Log("Error in ExecuteQuery", ex); throw new ApplicationException(ex.Message); } catch (Exception ex) { if (transaction != null) transaction.Rollback(); Log("Error in ExecuteQuery", ex); throw ex; } finally { CloseConnection(); } return rowCount; } public int ExecuteNonQuery(string sqlStmt, bool IsStoredProcedure) { OpenConnection(); int rowCount = 0; try { transaction = connection.BeginTransaction(); command.CommandText = sqlStmt; command.Connection = connection; if (IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; else command.CommandType = CommandType.Text; command.Transaction = transaction; rowCount = command.ExecuteNonQuery(); transaction.Commit(); } catch (SqlException ex) { if (transaction != null) transaction.Rollback(); throw new ApplicationException(ex.Message); } finally { CloseConnection(); command.Parameters.Clear(); } return rowCount; } public int ExecuteMulNonQuery(string sqlStmt) { int rowCount = 0; try { command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; rowCount = command.ExecuteNonQuery(); } catch (SqlException ex) { throw new ApplicationException(ex.Message); } return rowCount; } #endregion #region Execute Reader Methods public SqlDataReader ExecuteReader(string sqlStmt) { try { OpenConnection(); command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; reader = command.ExecuteReader(); } catch (SqlException ex) { throw new ApplicationException(ex.Message); } catch (Exception ex) { throw new ApplicationException(ex.Message); } return reader; } public SqlDataReader ExecuteReader(string sqlStmt, bool IsStoredProcedure) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; if (IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; else command.CommandType = CommandType.Text; reader = command.ExecuteReader(); } catch (SqlException ex) { throw new ApplicationException(ex.Message); } catch (Exception ex) { throw new ApplicationException(ex.Message); } return reader; } public Object ExecuteScalar(string sqlStmt) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; obj = command.ExecuteScalar(); } catch (SqlException ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); } return obj; } #endregion Reader Methods #region Adapter Methods public DataTable Adapter(string sqlStmt) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; adapter.SelectCommand = command; if (dataTable != null) dataTable.Clear(); adapter.Fill(dataTable); commandBld.DataAdapter = adapter; } catch (SqlException ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); } return dataTable; } public DataTable Adapter(string sqlStmt, bool IsStoredProcedure) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; if (IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; else command.CommandType = CommandType.Text; adapter.SelectCommand = command; if (dataTable != null) dataTable.Clear(); adapter.Fill(dataTable); commandBld.DataAdapter = adapter; } catch (SqlException ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); command.Parameters.Clear(); } return dataTable; } public void Adapter(string sqlStmt, string tableName, bool IsStoredProcedure) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; if (IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; else command.CommandType = CommandType.Text; adapter.SelectCommand = command; if (dataSet.Tables[tableName] != null) dataSet.Tables[tableName].Clear(); adapter.Fill(dataSet, tableName); commandBld.DataAdapter = adapter; } catch (SqlException ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); command.Parameters.Clear(); } } public void Adapter(string sqlStmt, string tableName) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; adapter.SelectCommand = command; if (dataSet.Tables[tableName] != null) dataSet.Tables[tableName].Clear(); adapter.Fill(dataSet, tableName); commandBld.DataAdapter = adapter; } catch (SqlException ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); } } public void Adapter(string sqlStmt, DataTable table) { try { OpenConnection(); command.CommandText = sqlStmt; command.Connection = connection; command.CommandType = CommandType.Text; adapter.SelectCommand = command; adapter.Fill(table); commandBld.DataAdapter = adapter; } catch (SqlException ex) { throw new ApplicationException(ex.Message); } catch (Exception ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); } } public void Adapter(string sqlStmt, DataTable table, bool IsStoredProcedure) { OpenConnection(); try { command.CommandText = sqlStmt; command.Connection = connection; adapter.SelectCommand = command; if (IsStoredProcedure) command.CommandType = CommandType.StoredProcedure; else command.CommandType = CommandType.Text; adapter.Fill(table); commandBld.DataAdapter = adapter; } catch (SqlException ex) { throw new ApplicationException(ex.Message); } catch (Exception ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); command.Parameters.Clear(); } } public void DataSetToDB(DataTable table) { OpenConnection(); try { adapter.Update(table); dataSet.AcceptChanges(); } catch (SqlException ex) { throw new ApplicationException(ex.Message); } finally { CloseConnection(); } } #endregion Adapter Methods #region Error Log Methods public static void Log(string Message) { Log(Message, null); } public static void Log(string Message, Exception Ex) { string fileName = Environment.CurrentDirectory + "\\" + "Error.log"; using (StreamWriter logFile = new StreamWriter(fileName, true)) { logFile.WriteLine("{0}: {1}", DateTime.Now, Message); if (Ex != null) logFile.WriteLine(Ex.ToString()); logFile.Close(); } } #endregion Error Log Methods } }
再请解释一下连接哪里错了?
Than please explain me where is the wrong to get the connection ?
更改此行:
change this line:
_connectionString = ConfigurationSettings.AppSettings["Server"];
至:
to:
_connectionString=ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
希望对您有所帮助:)
hope it helps :)
可能会有所帮助,
web.config中的应用设置 [ ^ ]
ConfigurationSettings类 [中需要使用基于您的web.Config的SqlConnectionString.
希望对您有帮助:)
It might be helpful,
AppSettings In web.config[^]
ConfigurationSettings Class[^]
In yourAppSettings["Server"]
need to use SqlConnectionString based on you web.Config.
Hope it helps you :)
这篇关于Web.config文件以获取连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文