gpt4 book ai didi

c# - 无法连接到 LocalDB

转载 作者:太空狗 更新时间:2023-10-29 20:36:10 28 4
gpt4 key购买 nike

我正在尝试运行一个 IIS 托管的应用程序(以电影数据库的形式)来自学如何设计一个 3 层架构程序:MVC 应用程序、WCF 服务、SQL Server 数据库。因为我没有完整的 SQL Server 的许可证 key ,所以我决定使用 LocalDB 并使用 WCF 服务来环绕我的存储过程。这是我第一次使用 LocalDB,在连接到我的实例 v13.0 时遇到了问题。

我的问题:

我这辈子都无法让我在 VS 中的项目连接到 LocalDB 实例。我不断收到以下错误:

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: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.

我的连接字符串(来自 Web.config):

<connectionStrings>
<add name="MovieDB" connectionString="Data Source=(localdb)\v13.0;Initial Catalog=MovieDB;Integrated Security=True;AttachDbFilename=|DataDirectory|\MovieDB.mdf" />
</connectionStrings>

使用 sqlcmd 连接到它并运行查询运行良好:

Connecting to LocalDB instance just fine from command line

Visual Studio SQL Server 对象资源管理器可以正常连接:

Visual SQL Server Object Explorer can connect just fine

连接字符串,当它到达有问题的行时由 Visual Studio 转义,所以我知道连接字符串没有被错误地转义:

"Data Source=(localdb)\\v13.0;Initial Catalog=MovieDB;Integrated Security=True;AttachDbFilename=|DataDirectory|\\MovieDB.mdf"

我尝试按照本指南(Part 1Part 2)进行操作,但图像至少在 2016 年 4 月 18 日之前已损坏,因此我无法完全理解我应该做什么。我只能收到上述错误,因为我将应用程序池标识更改为本地系统:

I was only able to get this error because I changed the app pool identity to LocalSystem

当应用程序池在 ApplicationPoolIdentity 身份下运行时,我收到此错误:

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: 50 - Local Database Runtime error occurred. Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user.

我什至修改了 C:\windows\system32\inetsrv\config\applicationHost.config 文件来强制加载配置文件。还是什么都没有。

<applicationPools>
<add name="DefaultAppPool" />
<add name="Classic .NET AppPool" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" />
<add name=".NET v2.0 Classic" managedRuntimeVersion="v2.0" managedPipelineMode="Classic" />
<add name=".NET v2.0" managedRuntimeVersion="v2.0" />
<add name=".NET v4.5 Classic" managedRuntimeVersion="v4.0" managedPipelineMode="Classic" />
<add name=".NET v4.5" managedRuntimeVersion="v4.0">
<!-- I added this, before it was an empty node -->
<processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
</add>
<applicationPoolDefaults managedRuntimeVersion="v4.0">
<!-- This was already here -->
<processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="false" />
</applicationPoolDefaults>
</applicationPools>

我的应用程序在“.NET 4.5”应用程序池下运行

My app is running under the ".NET 4.5" app pool

我的代码:

页面加载并调用此服务以获取数据库中的电影列表:

[ServiceContract]
public interface IMovieDBService
{
// ...

[OperationContract]
List<Movie> GetMovies(int actorID);

// ...
}

public class MovieDBService : IMovieDBService
{
// ...

public List<Movie> GetMovies(int actorID = 0)
{
var output = new List<Movie>();
using (var connection = DBTools.GetConnection(MovieDBConnectionName)) // Does not get past here; MovieDBConnection = "MovieDB"
{
var parameters = new List<SqlParameter>
{
new SqlParameter("@actorID", actorID)
};

using (var reader = connection.ExecuteStoredProcedure("GetMovies", parameters))
{
while (reader.Read())
{
output.Add(new Movie
{
ID = reader.GetInt32("ID"),
Title = reader.GetString("Title"),
Year = reader.GetInt16("Year"),
Genre = GetGenre(reader.GetInt32("GenreID"))
});
}
}
}

return output;
}

// ...
}

还有我的 DBTools 项目中有问题的方法:

public static SqlConnection GetConnection(string connectionName)
{
var connection = new SqlConnection();
var connectionString = ConfigurationManager.ConnectionStrings[connectionName];
if (connectionString != null)
{
connection.ConnectionString = connectionString.ConnectionString;
connection.Open(); // breaks here

return connection;
}

return null;
}

我只能假设问题与身份验证有关,或者是 IIS 托管的应用程序正在尝试连接到 LocalDB 用户进程这一事实,但我已经尝试按照其他问题的说明进行操作但没有成功(可能是因为未能理解真正的问题)。我很茫然……

最佳答案

试试这个:在您的 applicationhost 文件中尝试将 processModel identityType 更改为 SpecificUser 并包括用户名和密码。或者将您的应用程序池身份更改为有权访问数据库的用户。当您以应用程序池身份运行时,根据您的屏幕截图,您以本地系统运行 - 这是一个预定义的本地帐户,而 sqlcmd 以您登录的用户身份运行。

关于c# - 无法连接到 LocalDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36703854/

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