Click here to Skip to main content
15,887,379 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I tried to connect a C# console app to a .mdf database file, both created using MS tutorials as listed below.
The console window is blank and when I stop debugging I get an error msg as below.

What I have tried:

I created a database (Pubs) with a single table (Authors) in Visual Studio in a Forms project. All looks good. I created this database and table to match the Microsoft exercise in
https://support.microsoft.com/en-us/help/314145/how-to-populate-a-dataset-object-from-a-database-by-using-visual-c-net
I created a new C# Console app and copied and pasted the code from the above MS article.
I copied the 2 files Authors.mdf and Authors_log.ldf I created earlier into the same folder where Program.cs is located.
As no password or user id had been setup when creating the database, I tried the connection string with and without "Password=myPassword;User ID=myUserID;"
The code builds and stops with a blank cmd screen. I have to stop debugging, and then I see 10 lines informing me that various DLLs have loaded and the last line is
The program '[6096] ConsoleDBTest.exe' has exited with code -1 (0xffffffff).
I did get errors indicating the database file could not be found, and those have disappeared, so I thought I could assume that it wasn't a matter of file location.
Any suggestions as I have to do something with this tomorrow morning (Australian time).
Posted
Updated 14-May-18 2:19am
Comments
[no name] 9-May-18 11:19am    
We'd need to see your source code - first guess would be that the path to the .mdf file isn't correct.

OK, loads of problems here.

First off, your database should not be in the source code folder - at worst, it should be in the Bin/Debug and / or Bin/Release folder so it is easier to find - or better still in a data specific folder. This may help: Where should I store my data?[^]

Secondly, to use it from your own folder (even in a data folder) is a fairly poor idea - it means that your connection string needs to ATTACH the database so that SQL can use it, and that is only available for development, in the SQL Server Express editions - it isn't possible in production versions of the server software. You should pass control of the DB to SQL Server (easiest way: create it in SQL Server Management Console) and let it worry about where to keep it!

THen you need an SQL Server connection string, and that needs authentication, which for a local server may be pretty simple, or you may need an SQL User ID and Password combination to connect. This may help: Simple SQL Connection String Creation[^]

Once you have sorted that out, if it still doesn't work, show us the relevant code fragments and explain what happened.
 
Share this answer
 
Thanks to those who replied to my question. You made me think about what I was doing, that I needed to attach the database to the project, etc.
End result was that it all worked fine, and I have learned quite a lot as a consequence.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900