简体   繁体   English

无法连接到Sql Server Express数据库

[英]Unable to connect to Sql Server Express database

I installed Sql Server Express 2008 R2, and created a database file in Sql Server management Studio. 我安装了Sql Server Express 2008 R2,并在Sql Server Management Studio中创建了一个数据库文件。 Now From Visual Studio 2008 I try to connect, but it fails with error : 现在,在Visual Studio 2008年我尝试连接,但它失败, 错误

Unable to open the physical file "C:.....mdf". 无法打开物理文件“ C:..... mdf”。 Operating system error 32: "32(The process cannot access the file because it is being used by another process.)". 操作系统错误32:“ 32(该进程无法访问该文件,因为它正在被另一个进程使用。)”。 An attempt to attach an auto-named database for file C:....mdf failed. 尝试为文件C:.... mdf附加自动命名的数据库失败。 A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. 存在具有相同名称的数据库,或者无法打开指定的文件,或者该数据库位于UNC共享上。

...This is first time I use Express. ...这是我第一次使用Express。 I only used Sql Compact Edition before. 我以前只使用过Sql Compact Edition。 Are there some settings here I need to figure out? 我需要找出一些设置吗? It's not a duplicate name of other connection, and I have closed management studio completely. 它不是其他连接的重复名称,并且我已经完全关闭了Management Studio。 Still this error. 还是这个错误。 What am I missing..? 我在想什么..?

If there are some settings, please give a little detail how I access them, as I'm not used to dealing with databases. 如果有一些设置,请稍微详细说明如何访问它们,因为我不习惯于处理数据库。

SQL CE is a file based database - SQL Express isn't. SQL CE是基于文件的数据库-SQL Express不是。

It is a SQL Server - you can add it using the Server Explorer in Visual Studio. 它是一个SQL Server-您可以使用Visual Studio中的Server Explorer添加它。

The instance name is normally .\\SqlExpress . 实例名称通常为.\\SqlExpress

I faced the same issue today, and guess what daemon tools was the reason for this problem, so if you have installed daemon tools uninstall it and then try again. 今天,我遇到了同样的问题,并且猜测是什么守护程序工具导致了此问题,因此,如果您已安装守护程序工具,请先将其卸载,然后重试。 Hope it solves your problem too. 希望它也能解决您的问题。

You do not want to be connecting using the mdf. 您不想使用mdf进行连接。 What you want to do is Open a SqlClient.SqlConnection and then using the SqlClient.SqlCommand execute queries on the database, and then read the information using the SqlClient.DataReader , just like you would with A SqlCE database. 你想要做的就是打开SqlClient.SqlConnection ,然后使用SqlClient.SqlCommand对数据库执行查询,然后读取使用的信息SqlClient.DataReader ,就像你会与一个SQLCE数据库。

If you are trying to add it to the Server Explorer you just need to connect to the database just like you would in SSMS (Management Studio) and then you can use it in your project. 如果要尝试将其添加到服务器资源管理器中,则只需连接到数据库,就像在SSMS(Management Studio)中一样,然后可以在项目中使用它。

If you created a database in SQL Server, you probably have it attached. 如果在SQL Server中创建了数据库,则可能已附加了该数据库。 In Visual Studio you cannot access this database as a file. 在Visual Studio中,您不能作为文件访问此数据库。 You have to access it as an attached database (ie by its name). 您必须作为附加数据库访问它(即,按其名称)。

Generally you can use database in files as well in Express edition. 通常,您也可以在Express版本中在文件中使用数据库。 If you for some reason want to use file directly, then go to Management studio, right click on your database and detach it. 如果出于某种原因想要直接使用文件,请转到Management Studio,右键单击数据库并将其分离。 Then you can connect to it from Visual Studio on file basis. 然后,您可以基于文件从Visual Studio连接到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM