我自己写了一段数据库连接的代码,在析构函数里边调用了ADODB.Connection的close方法,不过不知道为什么没有关闭掉数据库连接啊。 class GF_ADODB
{
/// <summary>
/// 数据库连接字符串
/// </summary>
private string sqlstr; /// <summary>
/// 记录数据库连接的ADODB.Connection对象的实例
/// </summary>
private Connection conn = new Connection(); /// <summary>
/// 用于容纳一个来自数据库表的的ADODB.Connection对象的实例
/// </summary>
private ADODB.Recordset AdoRs = new Recordset(); /// <summary>
/// 初始化数据连接类GF_ADODB
/// </summary>
/// <param name="_Sqlstr">数据库连接字符串</param>
public GF_ADODB(string _Sqlstr)
{
this.sqlstr = _Sqlstr;
} /// <summary>
/// 析构函数
/// </summary>
~GF_ADODB()
{
try
{
MessageBox.Show("析构");
if (AdoRs == null)
{
AdoRs.Close();
AdoRs = null;
}
if (conn == null)
{
conn.Close();
}
}
catch { }
} /// <summary>
/// 获取或设置数据库连接字符串
/// </summary>
public string Sqlstr
{
set { sqlstr = value; }
get { return sqlstr; }
} /// <summary>
/// 获取记录数据库连接的ADODB.Connection对象的实例
/// </summary>
public Connection Conn
{
get { return conn; }
} /// <summary>
/// 获取一个ADODB数据库连接
/// </summary>
/// <returns>记录数据库连接的ADODB.Connection对象的实例</returns>
public Connection Gf_conn()
{
if (_Conn())
{
return conn;
}
else
{
return null;
}
} /// <summary>
/// 建立ADODB数据库连接
/// </summary>
/// <param name="conn">记录数据库连接的ADODB.Connection对象的实例</param>
/// <returns>返回一个布尔类型,成功建立数据库连接为true,否则为false</returns>
private Boolean _Conn()
{
try
{
conn.CommandTimeout = 10;
conn.ConnectionTimeout = 1;
conn.ConnectionString = sqlstr;
conn.Open(sqlstr, "", "", -1);
return true;
}
catch
{
try
{
Connection A = new Connection();
conn.CommandTimeout = 10;
conn.ConnectionTimeout = 1;
conn.ConnectionString = sqlstr;
conn.Open(sqlstr, "", "", -1);
return true;
}
catch (System.Exception e)
{
MessageBox.Show("数据库连接错误!!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
//try { conn.Close(); }
//catch { }
return false;
}
}
} public Boolean chaxun(string sql, DataGridView Dg, string bz)
{
try
{
if (conn.State == 0)
{
if (!_Conn()) { return false; }
} AdoRs.Open(sql, conn, CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockUnspecified, -1); if (!AdoRs.EOF & !AdoRs.BOF)
{
DataTable table = new DataTable();
table = Li.RecordsetTODataTable(AdoRs);
Dg.DataSource = table;
return true;
}
else
{
return false;
}
}
catch (System.Exception e)
{
MessageBox.Show(e.Message );
return false;
} }
}
{
/// <summary>
/// 数据库连接字符串
/// </summary>
private string sqlstr; /// <summary>
/// 记录数据库连接的ADODB.Connection对象的实例
/// </summary>
private Connection conn = new Connection(); /// <summary>
/// 用于容纳一个来自数据库表的的ADODB.Connection对象的实例
/// </summary>
private ADODB.Recordset AdoRs = new Recordset(); /// <summary>
/// 初始化数据连接类GF_ADODB
/// </summary>
/// <param name="_Sqlstr">数据库连接字符串</param>
public GF_ADODB(string _Sqlstr)
{
this.sqlstr = _Sqlstr;
} /// <summary>
/// 析构函数
/// </summary>
~GF_ADODB()
{
try
{
MessageBox.Show("析构");
if (AdoRs == null)
{
AdoRs.Close();
AdoRs = null;
}
if (conn == null)
{
conn.Close();
}
}
catch { }
} /// <summary>
/// 获取或设置数据库连接字符串
/// </summary>
public string Sqlstr
{
set { sqlstr = value; }
get { return sqlstr; }
} /// <summary>
/// 获取记录数据库连接的ADODB.Connection对象的实例
/// </summary>
public Connection Conn
{
get { return conn; }
} /// <summary>
/// 获取一个ADODB数据库连接
/// </summary>
/// <returns>记录数据库连接的ADODB.Connection对象的实例</returns>
public Connection Gf_conn()
{
if (_Conn())
{
return conn;
}
else
{
return null;
}
} /// <summary>
/// 建立ADODB数据库连接
/// </summary>
/// <param name="conn">记录数据库连接的ADODB.Connection对象的实例</param>
/// <returns>返回一个布尔类型,成功建立数据库连接为true,否则为false</returns>
private Boolean _Conn()
{
try
{
conn.CommandTimeout = 10;
conn.ConnectionTimeout = 1;
conn.ConnectionString = sqlstr;
conn.Open(sqlstr, "", "", -1);
return true;
}
catch
{
try
{
Connection A = new Connection();
conn.CommandTimeout = 10;
conn.ConnectionTimeout = 1;
conn.ConnectionString = sqlstr;
conn.Open(sqlstr, "", "", -1);
return true;
}
catch (System.Exception e)
{
MessageBox.Show("数据库连接错误!!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
//try { conn.Close(); }
//catch { }
return false;
}
}
} public Boolean chaxun(string sql, DataGridView Dg, string bz)
{
try
{
if (conn.State == 0)
{
if (!_Conn()) { return false; }
} AdoRs.Open(sql, conn, CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockUnspecified, -1); if (!AdoRs.EOF & !AdoRs.BOF)
{
DataTable table = new DataTable();
table = Li.RecordsetTODataTable(AdoRs);
Dg.DataSource = table;
return true;
}
else
{
return false;
}
}
catch (System.Exception e)
{
MessageBox.Show(e.Message );
return false;
} }
}
解决方案 »
- 用xenocode混淆后出现的问题,请教具体混淆方法。
- C# 鼠标点击textbox后,textbox内文本被全部选中或者消失为空
- C# DLL如何注入explorer.exe??
- 谢谢大家了!我写好了把汉字转变为拼音的方法!
- 请问 如何用 C# 向 access 的表中 插入(修改) 和 读取 类型为OLE对象(以图片为例吧)的字段 ?
- listbox 索引的问题。-------在线等
- 如何不重复显示同一个嵌套的子窗口
- 有谁知道怎么用c#读写word文件属性项里的摘要信息的
- 散散分,顺便讨论一下怎样设计一个优秀的OA产品!!!
- 200分救急,问题分成两步,分别给分---如何把webservice返回的数据集中的数据存到另立个数据表中
- winFrom qq空间登录
- Remoting的一个问题
或者using(SqlConnection con=...){...} ~GF_ADODB()这个要等到GF_ADODB对象被回收才会调用。
连接释放在这里肯定不行,不能及时释放资源
你的析构函数写的有点问题
/// <summary>
/// 析构函数
/// </summary>
~GF_ADODB()
{
try
{
MessageBox.Show("析构");
if (AdoRs != null)
{
AdoRs.Close();
AdoRs = null;
}
if (conn != null)
{
conn.Close();
}
}
catch { }
}
你的析构函数写的有点问题
/// <summary>
/// 析构函数
/// </summary>
~GF_ADODB()
{
try
{
MessageBox.Show( "析构 ");
if (AdoRs != null)
{
AdoRs.Close();
AdoRs = null;
}
if (conn != null)
{
conn.Close();
}
}
catch { }
}
ADODB.Connection里面就没有dispose方法,只有一个Close方法
然后我在CHAXUN这个函数里边调用Close方法也是不行
public Boolean chaxun(string sql, DataGridView Dg, string bz)
{
try
{
if (conn.State == 0)
{
if (!_Conn()) { return false; }
} AdoRs.Open(sql, conn, CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockUnspecified, -1); if (!AdoRs.EOF & !AdoRs.BOF)
{
DataTable table = new DataTable();
table = Li.RecordsetTODataTable(AdoRs);
Dg.DataSource = table;
if (AdoRs == null)
{
AdoRs.Close();
AdoRs = null;
}
if (conn == null)
{
conn.Close();
}
return true;
}
else
{
if (AdoRs == null)
{
AdoRs.Close();
AdoRs = null;
}
if (conn == null)
{
conn.Close();
}
return false;
}
}
catch (System.Exception e)
{
if (AdoRs == null)
{
AdoRs.Close();
AdoRs = null;
}
if (conn == null)
{
conn.Close();
}
MessageBox.Show(e.Message );
return false;
} }
在 finally中关闭。
或者using(SqlConnection con=...){...}~GF_ADODB()这个要等到GF_ADODB对象被回收才会调用。
连接释放在这里肯定不行,不能及时释放资源