class CommonConnection
{
public class dStructure
{
public static string ConnectionString = "";
}
public SqlConnection Conn;
#region "Connection Procedures"
public string ConnectionString
{
get
{
string sConn = string.Empty;
sConn = @"Server=ServerName;Initial Catalog=Database;User ID=userid;Password=password;";
dStructure.ConnectionString = sConn;
return dStructure.ConnectionString;
}
}
public void cnOpen()
{
try
{
if (Conn == null)
{
Conn = new System.Data.SqlClient.SqlConnection();
}
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = ConnectionString;
Conn.Open();
}
catch (SqlException e)
{
SqlConnection.ClearAllPools();
throw e;
}
catch (Exception ex)
{
throw ex;
}
}
public void cnClose()
{
try
{
if ((Conn != null))
{
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
Conn = null;
}
}
#endregion
public int ExecuteQuery(string strQuery, Int16 TimeOut = 30)
{
int RecordsAffected;
SqlCommand cmd;
try
{
cnOpen();
cmd = new SqlCommand(strQuery, Conn);
cmd.CommandTimeout = TimeOut;
RecordsAffected = cmd.ExecuteNonQuery();
return RecordsAffected;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cnClose();
cmd = null;
}
}
}
//尝试了另一个选项,如下所示,
public int ExecuteQuery(string strQuery, short TimeOut = 10)
{
SqlConnection NewConn = new SqlConnection();
try
{
if (NewConn == null)
{
NewConn = new System.Data.SqlClient.SqlConnection();
}
if (NewConn.State == ConnectionState.Open)
{
NewConn.Close();
}
NewConn.ConnectionString = "Server=ServerName;Initial Catalog=Database;User ID=userid;Password=password;";
NewConn.Open();
return new SqlCommand(strQuery, NewConn)
{
CommandTimeout = ((int)TimeOut)
}.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
NewConn.Close();
}
}
但仍然遇到同样的问题。 它的桌面应用程序,多线程。但是,当更多查询加载时,我不允许更改' ConnectionString'属性。连接的当前状态是开放的。 请注意,并非每次出现此问题时,仅在执行更多查询时才会出现。
//更新2 根据另一个问题的建议,我尝试使用下面的代码,但问题仍然存在。
public int ExecuteQuery(string strQuery, short TimeOut = 10)
{
int executeReader = 0;
try
{
using (SqlConnection connection = new SqlConnection(@"Server=Server;Initial Catalog=DB;User ID=id;Password=Password;"))
{
try
{
connection.Open();
SqlCommand command = new SqlCommand(strQuery, connection);
command.CommandType = CommandType.Text;
command.CommandTimeout = TimeOut;
executeReader = command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
}
return executeReader;
}
catch (Exception ex)
{
throw ex;
}
}
正如那里所建议的,使用命令使用默认的IDisposable,因此无需关闭连接。
答案 0 :(得分:1)
试试这个
<强>类别:强>
public class CommonConnection
{
String constr = System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;
public CommonConnection()
{
//
// TODO: Add constructor logic here
//
}
//Insert,Update,Delete....
public int ExecuteNonQuery1(string str)
{
//String constr = System.Configuration.ConfigurationManager.ConnectionStrings["CommonConnection"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(str, con);
int result = 0;
try
{
con.Open();
result = cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
result = -1;
try
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
catch (Exception ex2)
{
// ErrHandler.WriteError(ex2.ToString());
}
// ErrHandler.WriteError(ex.ToString());
}
return result;
}
}
<强> ASPX.CS:强>
SortedList s1 = new SortedList();
s1.Add("@mode", "Update");
s1.Add("@cid", ViewState["CategoryId"]);
int a = sp.ExecuteNonQuerySP1("SP_Name", s1);
if (a > 0)
{
}
答案 1 :(得分:1)
在你的两个代码中DEMO都是catch .. 让我们检查一下。
public void cnOpen()
{
try
{
if (Conn == null)
{
Conn = new System.Data.SqlClient.SqlConnection();
}
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = ConnectionString;
Conn.Open();
}`
假设if conn == null
然后它将进入块内并创建新连接,一切都很好。但是如果条件为假,那么它将不会创建新的sqlConnection
实例,如果条件
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
因为你告诉更多查询正在执行,所以也可能发生连接状态是除了打开之外的任何东西,如连接,提取,破坏等等,所以如果条件将是假的,如果有的话它们发生在ConnectionState.Open
之外,您的现有连接将不会关闭,并且它将进入它将遇到的下一行
Conn.ConnectionString = ConnectionString;
如果您的连接未关闭,则它将尝试更改现有连接(SqlConnection实例)连接字符串。如果没有处理实例,则无法更改。因此会抛出异常。
编辑尝试执行此类操作并从此代码块中删除Conn.open()
。
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
if (Conn == null)
{
Conn = new System.Data.SqlClient.SqlConnection();
Conn.ConnectionString = ConnectionString;
}
还需要在方法中的public int ExecuteQuery(string strQuery, Int16 TimeOut = 30)
中更新一件事。将此行Conn.Open();
放在cmd.CommandTimeout = TimeOut
;
public int ExecuteQuery(string strQuery, Int16 TimeOut = 30)
{
int RecordsAffected;
SqlCommand cmd;
try
{
cnOpen();
cmd = new SqlCommand(strQuery, Conn);
cmd.CommandTimeout = TimeOut;
Conn.Open(); //Add this here
RecordsAffected = cmd.ExecuteNonQuery();
return RecordsAffected;
}
catch (Exception ex)
{
throw ex;
}
finally
{
cnClose();
cmd = null;
}
}
}