c# winform网络断开重新连接后连接数据库的问题

来源:百度知道 编辑:UC知道 时间:2024/06/27 17:52:55
用的是定时器,如果网络一直保持连接状态,那么这段代码是没有问题的,但是一旦网络断开再连接上时,就显示错误:未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 未知模块 中。
其他信息: 系统错误。

private void timer1_Tick(object sender, System.EventArgs e)
{
this.sqlConnection1.ConnectionString = "packet size=4096;user id=******;data source=*************;persist security info=True;initial catalog=************;password=******";

this.sqlCommand1.Connection = this.sqlConnection1;
sqlConnection1.Open();
sqlCommand1.CommandText = "SELECT * FROM members where membername='abcd'";
sqlReader1 = sqlCommand1.ExecuteReader();
Read();
sqlReader1.Close();
sqlConnection1.Close();
在前面已经申明了sqlReader1
private System.Data.SqlClient.SqlDataReader sqlReader1;

private void timer1_Tick(object sender, System.EventArgs e)
{
try{
//你的代码
}
catch{}
}
因为如果网络断开,是可预知的异常,可以实现做好截获的机制.这样程序还能继续运行.

请问你的sqlReader声明了么