如何将现有数据库连接到新项目 [英] How do I connect an existing database to a new project

查看:163
本文介绍了如何将现有数据库连接到新项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将现有数据库连接到新项目。我打开新的表单项目(MovieList104)并尝试附加现有的数据库。我一直收到以下错误,无法弄清楚原因。



无法打开物理文件C:\ Data Files 092015 \DVD_List.mdf

操作系统错误5:'5 (访问被拒绝)'

尝试为文件D附加自动命名的数据库:\ Data Files \DVD_List.mdf失败。存在同名的数据库或指定的文件无法打开或者它位于UNC Share>



我还没有编写任何代码所以没有任何内容show。

我不知道出了什么问题。这是我第一次尝试附加这个文件,所以在那之后我就失去了!希望有人可以帮助我。

解决方案

如果您使用SQL,为什么要尝试附加数据库文件?

只需让SQL Server处理它,然后通过SQL连接它。



尝试使用Server Explorer窗格在VS中建立连接:

1)打开服务器资源管理器。

2)右键单击数据连接并选择添加连接

3)在对话框中接着,选择你的数据源和数据库,指定安全信息,然后按测试连接按钮。

4)连接工作时,按OK

5)在服务器资源管理器窗格中突出显示数据库,然后查看属性窗格。将显示连接字符串的工作示例,您可以将其复制并粘贴到您的应用程序或配置文件中。


成员10376725在解决方案1中写道:



我不知道为什么我之前没有注意到这一点,错误信息指的是一个名为<$ c $的数据库c> C:\ Data Files 092015 \DVD_List.mdf 但连接字符串中的文件名为 D:\ Data Files \DVD_List.mdf (这是正确的文件)。在这个新项目中没有<< u> b>我在 C:中指定了该文件,它甚至不再存在,它被复制到 D:\ Data Files 目录然后从 C:驱动器中删除。





请不要再那样了!不允许这种移动数据库文件的方法。请以正确的方式阅读此内容:移动数据库文件 [ ^ ]



最后,请按照以下步骤操作:如何:连接到数据库 [ ^ ]


I am having trouble connecting an existing database to a new project. I open the new forms project (MovieList104) and attempt to attach an existing database. I keep getting the following error and cannot figure out why.

"Unable to open the physical file C:\Data Files 092015\DVD_List.mdf
Operating System Error 5: ‘5(access Denied)’
An attempt to attach an auto-named database for file D:\Data Files\DVD_List.mdf failed. A database with the same name exists or specified file cannot be opened or it is located on UNC Share"

I haven’t written any code yet so there is nothing to show.
I have no idea what is wrong. It’s the first time I’ve tried to attach this file, so after that I’m LOST! Hopefully someone can help me.

解决方案

Why are you trying to attach a database file if you are using SQL?
Just let SQL Server handle it, and connect to it via SQL.

Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.


Member 10376725 wrote in commment to the solution 1:


I don't know why I didn't notice this before, the error message refers tp a database named "C:\Data Files 092015\DVD_List.mdf" but the file name in the connection string is "D:\Data Files\DVD_List.mdf"(which is the correct file). At no time in this new project have <<u>b>I designated the file in C: which doesn't even exist anymore it was copied to D:\Data Files directory then deleted from the C: drive.



Please, don't do that anymore! This method of moving database files is disallowed. Please, read this how to do that in proper way: Move Database Files[^]

Finally, follow this: How to: Connect to a Database[^]


这篇关于如何将现有数据库连接到新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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