简体   繁体   English

无法在本地连接到SQL Server

[英]Unable to connect to SQL Server locally

I am getting the following exception when I am trying to connect to a web service 尝试连接到Web服务时出现以下异常

System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file DatabaseName.mdb failed. System.Data.SqlClient.SqlException:尝试为文件DatabaseName.mdb附加自动命名的数据库失败。 A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. 存在具有相同名称的数据库,或者无法打开指定的文件,或者该数据库位于UNC共享上。

The connection string I am using is: 我正在使用的连接字符串是:

Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|WS_Assignment.mdb;uid=Admin;pwd=" 
providerName="System.Data.OleDb"

I am trying to access SQL Server on local system. 我正在尝试访问本地系统上的SQL Server。

I have gone through a lot of articles and followed all the different methods. 我阅读了很多文章,并遵循了所有不同的方法。 But I got no solution. 但是我没有解决办法。

Can anyone help me out? 谁能帮我吗?

In your Solution Explorer , click on "Show All Files". 在您的Solution Explorer ,单击"Show All Files". then go to your App_Data folder and delete WS_assignment.mdb and then run your application. 然后转到您的App_Data文件夹并删除WS_assignment.mdb ,然后运行您的应用程序。

Also, your provider is wrong 另外,您的提供者是错误的

providerName="System.Data.SqlClient" is the right one.

Secondly, your database name should end in .mdf or .sdf for SQL Server. 其次,对于SQL Server,数据库名称应以.mdf.sdf结尾。

So, your connection string will become: 因此,您的连接字符串将变为:

 <connectionStrings>
    <add name="ConnectionStringName"
     connectionString="Data Source=|DataDirectory|WS_Assignment.sdf"
     providerName="System.Data.SqlClient"/>
  </connectionStrings> 

Before Question was EDITED: 在问题编辑之前:

are you deploying on a web server , instead of your localhost. 您是在Web服务器上而不是localhost上进行部署。 If thats the case you need to publish the sql script in Server DB as servers dont allow attachDB file in your deployment and if thats the case then your problem is solved. 如果是这种情况,那么您需要在服务器数据库中发布sql脚本,因为服务器不允许您的部署中使用attachDB文件,如果是这种情况,那么您的问题就可以解决。

After Update you can always try with fullpath and yes you need OLEDB call not sql make sure thats not the case with your .cs code 更新后,您始终可以尝试使用全路径,是的,您需要OLEDB调用而不是sql,请确保.cs代码不是这种情况

Correction in your question: 您的问题更正:

i am trying to access SQL server On local system 我正在尝试访问本地系统上的SQL Server

: i dont see how you can do that with .mdb file; :我看不到如何使用.mdb文件来实现; even if thats not the case make sure sql services are running properly in your system go to->start button->program files->microsoft sql server yourversion-> configuration manager-> check running services. 即使不是这种情况,请确保sql服务在您的系统中正常运行,请转至->“开始”按钮->程序文件-> Microsoft sql服务器yourversion->配置管理器->检查正在运行的服务。

Check this. 检查一下。
http://msdn.microsoft.com/en-IN/library/5ybdbtte%28v=vs.71%29.aspx http://msdn.microsoft.com/zh-CN/library/5ybdbtte%28v=vs.71%29.aspx
using Access requires System.Data.OleDb library 使用Access需要System.Data.OleDb

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

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