0

I'm trying to create a login page.

I get the users ID and password as parameters, and I want to access my database and compare them, but when I execute the query using ExecuteReader, the query always times out.

The database itself is very very small, only with about 5 or 6 users so it shouldn't be timing out......

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();

The reader will timeout before entering the 'while loop'

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user74671
  • 21
  • 4
  • Try using the debugger to see what's going on. Also, have a look at `using` for the SQL part, so that it autocloses your connection for you (it implements `IDisposeable` - as per here: https://stackoverflow.com/questions/16985876/sqlconnection-sqlcommand-sqldatareader-idisposable) – Syntax Error Jun 01 '19 at 11:38

1 Answers1

0

According to MSDN, ExecuteReader()

Sends the CommandText to the Connection and builds a SqlDataReader.

Generally, if you get a timeout here, the problem is with the database connection. Looking at your code, I think the connection string in the connetionString is the problem. Check to see if it really is the connection string you're looking for. Also check for invisible characters in the same connection string.

Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34