【问题标题】:ExecuteReader Timing OutExecuteReader 超时
【发布时间】:2019-06-01 21:05:02
【问题描述】:

我正在尝试创建一个登录页面。

我将用户 ID 和密码作为参数,我想访问我的数据库并比较它们,但是当我使用 ExecuteReader 执行查询时,查询总是超时。

数据库本身非常非常小,只有大约 5 或 6 个用户,所以应该不会超时......

SqlConnection cnn = null;
string connectionString = null;

connectionString = @"Data Source=DESKTOP-A5GR284\SQLEXPRESS;Initial Catalog=Chat_DB;Integrated Security=True";

cnn = new SqlConnection(connectionString);
cnn.Open();

SqlCommand cmd = new SqlCommand("SELECT * FROM Users", cnn);

SqlDataReader reader = cmd.ExecuteReader();

while (reader.HasRows)
{
    while (reader.Read())
    {
         Console.WriteLine("{0}", reader.GetString(1));
    }

    reader.NextResult();
}

cnn.Close();

阅读器在进入'while循环'之前会超时

【问题讨论】:

  • 尝试使用调试器看看发生了什么。此外,请查看 SQL 部分的using,以便它为您自动关闭您的连接(它实现了IDisposeable - 在这里:stackoverflow.com/questions/16985876/…)

标签: c# sql-server executereader


【解决方案1】:

根据MSDN,ExecuteReader()

将CommandText 发送到Connection 并构建SqlDataReader。

通常,如果您在此处遇到超时,则问题出在数据库连接上。查看您的代码,我认为connetionString 中的连接字符串是问题所在。检查它是否真的是您正在寻找的连接字符串。还要检查同一连接字符串中的不可见字符。

【讨论】:

  • 谢谢!问题确实是数据库连接。
  • 很高兴能为您提供帮助。如果您认为答案有用,请点赞,以便其他人也能得到帮助。
猜你喜欢
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多