C#(将SQL语句写在类里面)布置为何关闭conn出问题(急~~~)
publicclassSqlDBConnect{SqlConnectionconn;///<summary>///打开数据库///</summary>publicvoid...
public class SqlDBConnect
{
SqlConnection conn;
/// <summary>
/// 打开数据库
/// </summary>
public void Open()
{
if (conn == null)
{
string strcon = ConfigurationManager.AppSettings["Data"].ToString(); try
{
conn = new SqlConnection(strcon);
conn.Open();
}
catch (Exception ex)
{
}
}
}
/// <summary>
/// 关闭数据库
/// </summary>
public void Close()
{
if (conn != null)
{
try
{
//判断连接的状态是否打开
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
catch (Exception ex)
{ }
}
}
/// <summary>
/// Sql命令语句
/// </summary>
/// <param name="sqlstr"></param>
public void SqlCommand(string sqlstr)
{
if (sqlstr != null)
{
Open();
try
{
SqlCommand com = new SqlCommand(sqlstr, conn);
com.ExecuteNonQuery();
sqlstr = null;
}
catch (Exception ex)
{
}
finally
{
Close();
}
}
}
}
在conn.Close();出问题如下:
ServerVersion = “conn.ServerVersion”引发了“System.InvalidOperationException”类型的异常
连接的时候没有异常,我查看过了,就是在关闭后异常 展开
{
SqlConnection conn;
/// <summary>
/// 打开数据库
/// </summary>
public void Open()
{
if (conn == null)
{
string strcon = ConfigurationManager.AppSettings["Data"].ToString(); try
{
conn = new SqlConnection(strcon);
conn.Open();
}
catch (Exception ex)
{
}
}
}
/// <summary>
/// 关闭数据库
/// </summary>
public void Close()
{
if (conn != null)
{
try
{
//判断连接的状态是否打开
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
catch (Exception ex)
{ }
}
}
/// <summary>
/// Sql命令语句
/// </summary>
/// <param name="sqlstr"></param>
public void SqlCommand(string sqlstr)
{
if (sqlstr != null)
{
Open();
try
{
SqlCommand com = new SqlCommand(sqlstr, conn);
com.ExecuteNonQuery();
sqlstr = null;
}
catch (Exception ex)
{
}
finally
{
Close();
}
}
}
}
在conn.Close();出问题如下:
ServerVersion = “conn.ServerVersion”引发了“System.InvalidOperationException”类型的异常
连接的时候没有异常,我查看过了,就是在关闭后异常 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询