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,因此无需关闭连接。
最佳答案
尝试这个
类:
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)
{
}