我用sqlserver2008和c#winform(直接连接)创建了一个程序。
当我首先启动程序时,它运行良好,连接没有问题。 但是在没有使用该程序几个小时或几天(无法确切检查)之后,Sqlconnection.open()突然抛出一个异常。 我猜这与连接池或其他东西有关,但我没能在谷歌或这里搜索。
以下是我的错误消息和连接代码。
ERROR: (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error:) An error has occurred while establishing a connection to the server. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
location: System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling) location: System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) location: System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) location: System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) location: System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) location: System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) location: System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) location: System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) location: System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) location: System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) location: System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) location: System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) location: System.Data.SqlClient.SqlConnection.Open() location: DHP_QRChecker.Module.clsDBMgr.Open(String query)
代码:
class clsDBMgr
{
private SqlConnection conn;
private string strConn = "";
//prepare connection string
public clsDBMgr()
{
strConn = "server=" + Constants.g_strDB_Addr + ";database=" + Constants.g_strDB_Name
+ ";User ID=" + Constants.g_strDB_ID + ";Password=" + Constants.g_strDB_PW;
}
//try open once and close
//create and return SqlCommand object
public SqlCommand Open(string query)
{
SqlCommand cmd = null;
try
{
conn = new SqlConnection(strConn);
cmd = new SqlCommand(query, conn);
**conn.Open(); //error occurs suddenly**
}
catch (Exception _error)
{
clsFunc.MsgErr(_error.Message, _error);
return null;
}
finally
{
Close();
}
return cmd;
}
//execute query.
//at finally block, always call SqlConnection.Close();
public DataRow[] Execute(string query)
{
SqlCommand cmd = null;
DataRow[] row = null;
try
{
cmd = Open(query);
cmd.CommandTimeout = 0;
if (cmd == null)
throw new Exception("can't connect to db.");
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);
if (ds == null || ds.Tables.Count == 0)
throw new Exception("can't fill");
DataTable dt = ds.Tables[ds.Tables.Count - 1];
row = dt.Select();
if (!IsValid(row))
throw new Exception("데이터베이스 정보가 유효하지 않습니다.");
}
catch (Exception _error)
{
string errorMsg;
errorMsg = _error.Message;
clsFunc.MsgErr(errorMsg, _error);
row = null;
}
finally
{
Close();
}
return row;
}
//Close function
public void Close()
{
if (conn != null)
conn.Close();
}
private bool IsValid(DataRow row)
{
if (row == null || row.ItemArray.Length == 0 || row[0] == null)
return false;
else
return true;
}
}