[英]How do I create a generic file path to a local database in a C# Application?
I have a simple data entry Windows Form with a datagridview display that links to a local database.我有一个简单的数据输入 Windows 表单,带有链接到本地数据库的 datagridview 显示。 When I run the program and try to add data on another computer, I get this message:当我运行程序并尝试在另一台计算机上添加数据时,我收到以下消息:
Unhandled exception has occurred in your application.您的应用程序中发生了未处理的异常。 If you click Continue, the application will ignore this error and attempt to continue.如果单击继续,应用程序将忽略此错误并尝试继续。 If you click Quit, the application will close immediately.如果单击退出,应用程序将立即关闭。
An attempt to attach an auto-named database for file C:\Users\roberto.yepez\Documents\Visual Studio\2010\Projects\Financial Aid Calculator\Financial Aid Calculator\StudentInfo1.mdf failed.尝试为文件 C:\Users\roberto.yepez\Documents\Visual Studio\2010\Projects\Financial Aid Calculator\Financial Aid Calculator\StudentInfo1.mdf 附加自动命名的数据库失败。 A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."存在同名数据库,或无法打开指定文件,或位于 UNC 共享上。”
The file path is to the path on the computer where I coded the program.文件路径是我编写程序的计算机上的路径。
Here is my code:这是我的代码:
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\roberto.yepez\Documents\Visual Studio 2010\Projects\Financial Aid Calculator\Financial Aid Calculator\StudentInfo1.mdf';Integrated Security=True".ToString());
I am a self-taught coder, please help: :)我是一个自学成才的程序员,请帮忙::)
I believe you're running into a problem because your local sql server to which your code is trying to attach the StudentInfo1.mdf (whose path is in the connection string) already contains a database called StudentInfo1 - it decided to try and create a database of this name based on the name of the mdf file.我相信您遇到了问题,因为您的代码尝试将 StudentInfo1.mdf(其路径在连接字符串中)附加到的本地 sql 服务器已经包含一个名为 StudentInfo1 的数据库 - 它决定尝试创建一个数据库此名称的名称基于 mdf 文件的名称。 I'm guessing that you can pick your own name by specifying Initial Catalog in your connection string but this would mean there are two databases with possibly the same set of tables and a potential for confusion我猜您可以通过在连接字符串中指定 Initial Catalog 来选择自己的名称,但这意味着有两个数据库可能具有相同的表集并且可能会造成混淆
Per the comment I posted I would instead advocate that you use SQL Server Management Studio to permanently attach your db (you make have already done this) and then adjust your connection string so that it refers to the permanently attached db.根据我发布的评论,我建议您使用 SQL Server Management Studio 永久附加您的数据库(您已经这样做了),然后调整您的连接字符串,使其引用永久附加的数据库。 This reduces the chances that your next question will be "my code says it's updating my db but I cannot see any changes??"这减少了您的下一个问题是“我的代码说它正在更新我的数据库但我看不到任何更改??”的可能性。
Please move this connection string请移动此连接字符串
"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\roberto.yepez\Documents\Visual Studio 2010\Projects\Financial Aid Calculator\Financial Aid Calculator\StudentInfo1.mdf';Integrated Security=True" “数据源=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\roberto.yepez\Documents\Visual Studio 2010\Projects\Financial Aid Calculator\Financial Aid Calculator\StudentInfo1.mdf';Integrated Security=True”
to app.config file .到app.config 文件。 When you deploy to production, change the paths in that app.config, according to the production machine settings.部署到生产环境时,根据生产机器设置更改该 app.config 中的路径。
You can even apply transformations on the app.config to deploy in various machines/scenarios.您甚至可以在 app.config 上应用转换以部署在各种机器/场景中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.