c# - 无法连接到 Visual Studio 2012 Express For Web 中的本地数据库 : (error 26)(C#)(SQL Server 2012)

标签 c# asp.net .net sql-server

最近,我制作了一个带有数据库的简单网站(在 App_Data 文件夹中)。我使用以下方法创建了一个类文件(在 App_Code 文件夹中),该方法将创建与数据库的连接:

public static SqlConnection ConnectToDb(string dbFileName)
{
    string path = HttpContext.Current.Server.MapPath("App_Data/");
    path += dbFileName;
    string connString =  @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + 
                         path + 
                         ";Integrated Security=True;User Instance=True";
    SqlConnection conn = new SqlConnection(connString);
    return conn;

}

当我需要打开该连接时,我收到以下消息:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

我不明白的是,我在一年多前在 Visual Studio 2010 中构建的另一个网站中使用了 -same- 方法,该方法运行良好。

检查了路径,正确,与参数中的数据库文件名一致。

代码或 VS 的设置有什么问题?提前致谢。

编辑:只是尝试使用 (localDB)\v11.0 而不是 .\SQLEXPRESS 现在我收到此错误消息:

The user instance login flag is not allowed when connecting to a user instance of SQL Server.

最佳答案

您收到该错误是因为 (localDB)\v11.0 已经是 SQL Server 的用户实例。当您使用 SQLEXPRESS SQL Server 实例时,情况并非如此。因此,只需删除连接字符串上的 User Instance=true 即可。

关于c# - 无法连接到 Visual Studio 2012 Express For Web 中的本地数据库 : (error 26)(C#)(SQL Server 2012),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18558408/

相关文章:

c# - 如何从 C++ dll 在 C# 函数中传递大小未知的数组?

asp.net - ubuntu上从 Rider 到mircorsoft sql server的连接字符串

c# - aspx页面超时过期错误

c# - ASP.NET Webforms - 如何确定请求以问号结尾,即。?

c# - 如何在显示 ContextMenuStrip 时接收鼠标移动事件?

c# - 如何确保 c# 解决方案中的所有项目都使用相同版本的第三方库?

C# Select Nodes 找不到节点

C# 存储数字的安全方式?

c# - Entity Framework : How to enable cascade delete on one way related entities

.net - .Net 中的异步 BinaryReader 和 BinaryWriter?