简体   繁体   English

使用Visual Studio和NUnit连接到localdb

[英]Connecting to localdb with Visual Studio and NUnit

I'm trying to run unit tests out of NUnit that are centered around databases. 我试图从围绕数据库的NUnit中运行单元测试。 The tests just access the localdb but NUnit keeps giving errors. 测试仅访问localdb,但NUnit不断出错。 I've set up a localdb (or so I think), but the sql command isn't recognizing it. 我已经设置了localdb(或者我认为是),但是sql命令无法识别它。

Exception occurred while deleting from table: A network-related or instance-specific error occurred while establishing a connection to SQL Server. 从表中删除时发生异常:建立与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. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist. (提供者:SQL网络接口,错误:50-发生本地数据库运行时错误。指定的LocalDB实例不存在。

Here is the path for the sql command in the unit test: 这是单元测试中sql命令的路径:

private const string ConnectionString_ = "Data Source=(localdb)\\Projects;Initial Catalog=MetricsBusDatabase;Integrated Security=SSPI;";

I've read over most of the related sql questions and I'm currently reinstalling the sql server. 我已经阅读了大多数相关的sql问题,目前正在重新安装sql服务器。 No one seems to be getting the same error. 似乎没有人遇到相同的错误。 I suspect I've missed something very simple. 我怀疑我错过了一些非常简单的事情。

The connection string does not look right. 连接字符串看起来不正确。 I do not see the "AttachDbFilename" part which points to the mdf file which represents the local db. 我看不到“ AttachDbFilename”部分,该部分指向代表本地数据库的mdf文件。

Try this: 尝试这个:

connectionString=@"Data Source=(LocalDb)\v11.0;AttachDbFilename=PATH_TO_MDF.mdf;Initial Catalog=MetricsBusDatabase;Integrated Security=SSPI;";

Or see this response to a similar problem. 或查看响应以解决类似问题。

I think you should change the Data Source to (localdb)\\v11.0. 我认为您应该将数据源更改为(localdb)\\ v11.0。 If you are using VS2015, you can use (localdb)\\MSSQLLocalDB as Data Source. 如果使用的是VS2015,则可以使用(localdb)\\ MSSQLLocalDB作为数据源。 So in your case it should be the following. 因此,在您的情况下应该是以下情况。

private const string ConnectionString_ = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MetricsBusDatabase;Integrated Security=SSPI;";

or 要么

private const string ConnectionString_ = "Data Source=(localdb)\\v11.0;Initial Catalog=MetricsBusDatabase;Integrated Security=SSPI;";

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

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