gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:34 25 4
gpt4 key购买 nike

最近,我制作了一个带有数据库的简单网站(在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com