我尝试从数据库中查询数据,有时我在日志中出现此错误:我得到这个SQL传输级错误,但不是所有的时间
A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The handle is invalid.) The code is inside a try-catch block. What is weird is that i do not get this error all the time. The catch block does get executed.
然而,不管我得到的错误或不,下拉列表我试图填充不填充。好像没有数据从查询中返回。
我正在连接数据库,数据在那里,我可以打开连接并运行查询到数据库。数据库与运行代码的位置在同一台机器上。
另外,代码工作时,我连接到服务器上的数据库,但我试图使本地数据库工作时,我没有连接到服务器。当我想从本地数据库运行时,我只需要更改连接字符串。
下面是查询数据库
public static void QueryDB(string query)
{
cmd = new SqlCommand();
cmd.CommandText = query;
cmd.Connection = MyConn;
if (dr != null) dr.Dispose();
dr = cmd.ExecuteReader();
}
这里是连接字符串连接到数据库的功能:
MyConn = new SqlConnection("Data Source=tcp:localhost; Database=EscalationManagementSystem; Integrated Security=true;");
是否有错设置或可这是一个错误在代码中。
在此先感谢。
既然您没有给我们任何代码来审查有关造成这个问题的假设是不可靠的。 – 2012-02-02 18:14:30
我猜'dr'是'SqlDataReader'的一些痛处?这是一个全球变量? – 2012-02-02 18:24:07
对不起,是'dr'是'SqlDataReader'。 – DFord 2012-02-02 18:24:57