- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试运行一个 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 连接到它并运行查询运行良好:
Visual Studio SQL Server 对象资源管理器可以正常连接:
连接字符串,当它到达有问题的行时由 Visual Studio 转义,所以我知道连接字符串没有被错误地转义:
"Data Source=(localdb)\\v13.0;Initial Catalog=MovieDB;Integrated Security=True;AttachDbFilename=|DataDirectory|\\MovieDB.mdf"
我尝试按照本指南(Part 1 和 Part 2)进行操作,但图像至少在 2016 年 4 月 18 日之前已损坏,因此我无法完全理解我应该做什么。我只能收到上述错误,因为我将应用程序池标识更改为本地系统:
当应用程序池在 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”应用程序池下运行
我的代码:
页面加载并调用此服务以获取数据库中的电影列表:
[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/
我在集成测试环境中使用 LocalDb - 在测试运行之前和之后实例化和处理我的实例。 但是,当我在我的实例中创建一个数据库时,它仍然会将我的表和数据刷新到磁盘。是否可以在“内存中”模式下运行 Loc
我使用的是 SQL Server 2014 Express 和 LocalDB 选项,并且 Visual Studio 2013 中的 SQL Server 对象资源管理器 Pane 中有以下内容..
重现步骤。 列出现有实例: > sqllocaldb info v11.0 停止指定名称的实例: > sqllocaldb stop "v11.0" Stop of LocalDB instance
我无法在开发人员机器上添加数据库。 我正在运行 win 10,visual studio 2015。 我重新安装了 SQL Server 2016 两次,上次禁用了防火墙,最后都给了绿色标记。 虽然我
我将VS2012和SSME 2012与localdb一起安装,发现有两台服务器可以连接到:(localDB)\v11.0和(localDB)\Projects。 我必须使用它们中的哪一个来执行,它们分
我们的应用程序使用 SQL Server LocalDb 2014 作为数据库引擎。我们使用的连接字符串是 "Data Source=(localdb)\MSSQLLOCALDB;Initial Ca
我刚刚完成了 VS2015 的全新安装 更新 2 在一个新工作站上,并开始运行一些使用 SQL LocalDb 的集成测试。他们失败了以下 SqlException : Additional info
当我尝试从 C# 访问数据库时,出现此异常。 我的连接字符串是 Data Source=(localdb)\v11.0;integrated security=true;User Instance =
我使用 SqlLocalDb.msi 包安装了 LocalDb,并且可以使用服务器名称 (LocalDb)\v11.0 使用 SSMS 连接到它。到目前为止,一切都很好。问题是,当我尝试通过 .NET
我正在尝试在本地复制生产环境,而生产数据库使用链接服务器。我已经能够创建 localdb 的多个实例;是否可以在 localdb 实例之间创建链接服务器?如果没有,我还有哪些其他选项可用(理想情况下不
我首先使用实体框架 6 代码。 目前我有一个用于生产的数据库。我想为测试添加一个不同的数据库。我怎么做? 我的测试项目中是否需要另一个连接字符串? 这两个连接字符串应该有什么区别? 我尝试复制连接
我以前从未在应用程序中使用过 LocalDB。我有两个问题: 1) LocalDB 仅用于测试还是有时会在实际环境中推出?2) 我注意到连接字符串因 PC 而异。例如,请查看下面适用于我的台式电脑(使
我正在尝试运行一个 IIS 托管的应用程序(以电影数据库的形式)来自学如何设计一个 3 层架构程序:MVC 应用程序、WCF 服务、SQL Server 数据库。因为我没有完整的 SQL Server
我正在尝试在 Ubuntu 17.04 上启动 .Net Core 2.0 应用程序。我之前在 Windows 10 上开发过它并且运行良好。问题是,当我运行 dotnet ef database u
我正在尝试为我的 ASP.NET Web API 项目编写单元/集成测试,并努力单独运行每个测试。请允许我解释一下。 我有一个配置了部署设置的 *.testsettings 文件。在每次测试运行之前,
我正在尝试为我的 Windows 窗体应用程序创建备份和恢复功能。 因此,我尝试进行数据库恢复。我的代码是这样的: string cbdfilename = "c:\\Users\\Public\\P
如何删除已删除其文件的 SQL LocalDB 数据库? 删除数据库会产生以下消息: Unable to open the physical file "C:\Users\Public\Documen
我有以下 F# 脚本。我从正在运行的 Asp.net Mvc 项目复制了连接字符串。但是,它出现以下错误。连接字符串应该正确,因为 Asp.Net Mvc 项目工作正常。或者 F# 类型提供程序不接受
我使用本地开发的网站收集了一堆数据。现在我需要分析数据,但我似乎无法找到我的网站使用的 LocalDB 数据库的 .mdf 文件。 查看数据连接,显示myusername\localdb#abunch
我有一个像这样的 VS 2012 解决方案设置: 英孚模型项目 EF模型测试项目 ASP.NET MVC 4 应用程序 WCF数据服务项目 在开发过程中,我想使用 LocalDB 作为 EF 的后备数
我是一名优秀的程序员,十分优秀!