关于.net,c#中的一个手动关闭数据库的问题

来源:百度知道 编辑:UC知道 时间:2024/05/24 08:29:04
请看:我的公用的连接数据库的类 有这么2个方法

protected void Open()
{
if (Connection == null)
{
try
{
Connection = new OdbcConnection(ConnectionString);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
if (Connection.State.Equals(ConnectionState.Closed))
{
try
{
Connection.Open();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
}
public void Close()
{
try
{
if (Connection != null)
Connection.Close(

DataReader使用这个方法打开
cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

System.Data.CommandBehavior.CloseConnection表示关闭连接时同时关闭DataReader

所以只需要database.Close();

你应该从内到外依次关闭

先dr.Close()
再database.Close();

这是通常的做法

如果直接database.Close()是否对其他的数据库访问有影响
先dr.Close() 在最后在 database.Close()