且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将SQL mdf文件连接到C#控制台应用程序?

更新时间:2023-02-08 17:52:31

好的,这里有很多问题。



首先,你的数据库不应该在源代码文件夹中 - 最糟糕的是,它应该在Bin / Debug和/或Bin / Release文件夹中,这样更容易找到 - 或者更好的仍然在数据特定文件夹中。这可能会有所帮助:我应该在哪里存储数据? [ ^ ]



其次,从你自己的文件夹(甚至在数据文件夹中)使用它是一个相当糟糕的想法 - 这意味着你的连接字符串需要ATTACH数据库,以便SQL可以在SQL Server Express版本中使用它,并且只能用于开发 - 在服务器软件的生产版本中是不可能的。您应该将数据库的控制权传递给SQL Server(最简单的方法:在SQL Server管理控制台中创建它)并让它担心在哪里保留它!



你呢需要SQL Server连接字符串,并且需要身份验证,对于本地服务器可能非常简单,或者您可能需要SQL用户ID和密码组合才能进行连接。这可能有所帮助:简单的SQL连接字符串创建 [ ^ ]



一旦你对它进行了排序,如果它仍然不起作用,请向我们展示相关的代码片段并解释发生了什么。


感谢那些回复的人我的问题。你让我思考我在做什么,我需要将数据库附加到项目等等。

最终结果是它一切正常,我学到了很多因素

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).

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.


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.