0

As an exercise, I've been trying to build the database application described in the first chapter of an earlier edition (the 2nd) of Head First C#. (Chapter 1 on O'Reilly website).

I've had to make some modifications because said book is rather ancient, but now it works. It has a SQL Server (LocalDB) database, ContactDB.mdf, containing only one table, People.

Definition of People table

The only thing is, every time I run the exe for the first time (after starting up Windows), I get a SqlException mentioning a time-out. The message is in Dutch, and I would put the English equivalent of it here if I could find it. But despite the same error number, the messages I encounter on the web are different. The message I get refers to a handshake (again, sorry for the language):

System.Data.SqlClient.SqlException (0x80131904): Connection timeout exceeded. The timeout period has elapsed when attempting to use the pre-login handshake confirmation. A possible cause is that the pre-login handshake failed or the server was unable to respond in time. The time spent trying to connect to this server was - [Pre-Login]-initialization=16271; handshake=0;

System.ComponentModel.Win32Exception (0x80004005): Request timed out

If I close the application and start it a second time, I don't get the error.

Doing some research, I find that possible causes for a time-out can be a query that takes a long time (my People table contains 6 rows) or a firewall issue. Can it be a firewall problem if the application always runs fine the second and subsequent times?

Or should I set a different time-out value?

Any thoughts would be much appreciated.

  • All content of a question must be in English, that includes errors. – Thom A Jun 30 '21 at 15:29
  • maybe the system simply takes that long to open the DBF and be in a queryable state with a usable SQL Server engine instance? – Marc Gravell Jun 30 '21 at 15:32
  • @Larnu It's a drag that messages are not in English anyway. I will try to get rid of the language packs responsible for this behaviour and report back. – sandokanfirst Jun 30 '21 at 15:40
  • @Marc Gravell I suppose I could adjust the time-out, but I wonder if that is good practice? What if I installed this on someone else's (slower) PC, wouldn't I run the risk of encountering time-outs again? Or would it be best to catch the exception? I'm really a novice at SQL Server, so I have no idea how to go about this. – sandokanfirst Jun 30 '21 at 15:47
  • Are you using SQL Server Express or LocalDB? – Charlieface Jun 30 '21 at 16:06
  • @Charlieface Oops. Contrary to what I wrote in my original post, I now think that this is LocalDB. At least the connection string at least is "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\users\\sources\repos\Contacts\ContactDB.mdf;Integrated Security=True". I will edit my question accordingly. – sandokanfirst Jun 30 '21 at 16:26
  • LocalDB does take a little bit of time to load, so it can occasionally cause timeouts, [you may want to prevent auto-shutdown](https://stackoverflow.com/questions/14153509/how-to-prevent-sql-server-localdb-auto-shutdown). Also [`AttachDbFilename` is a very bad idea](https://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename) – Charlieface Jun 30 '21 at 18:54
  • @Charlieface I'll look into the auto-shutdown (or the prevention of it). I didn't like the idea of that hard-coded connection string (which honestly is just the result of clicking some controls and adding some properties), so thanks for pointing me in the right direction. – sandokanfirst Jun 30 '21 at 19:13

0 Answers0