如何将SQL mdf文件连接到C#控制台应用程序? [英] How do I connect an SQL mdf file to C# console app?

查看:99
本文介绍了如何将SQL mdf文件连接到C#控制台应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将C#控制台应用程序连接到.mdf数据库文件,这两个文件都是使用下面列出的MS教程创建的。

控制台窗口为空,当我停止调试时出现错误信息如下所示。



我尝试过:



我创建了一个在Forms项目中的Visual Studio中具有单个表(作者)的数据库(Pubs)。一切看起来都不错我创建了这个数据库和表来匹配微软的练习

https://support.microsoft.com/en-us/help/314145/how-to-populate-a-dataset-object-从数据库中使用visual-c-net

我创建了一个新的C#Console应用程序,并复制并粘贴了上述MS文章中的代码。

我将之前创建的2个文件Authors.mdf和Authors_log.ldf复制到Program.cs所在的同一个文件夹中。

由于在创建数据库时没有设置密码或用户ID,我尝试过带或不带Password = myPassword; User ID = myUserID;的连接字符串

代码使用空白cmd屏幕构建和停止。我必须停止调试,然后我看到10行通知我各种DLL已加载,最后一行是

程序'[6096] ConsoleDBTest.exe'已退出,代码为-1(0xffffffff )。

我收到的错误表明无法找到数据库文件,而且这些文件已经消失,所以我想我可以认为这不是文件位置的问题。

任何建议,因为我必须在明天早上做这件事(澳大利亚时间)。

解决方案

好的,这里有很多问题。



首先,你的数据库不应该在源代码文件夹中 - 最糟糕的是,它应该在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.


这篇关于如何将SQL mdf文件连接到C#控制台应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆