c# - 带有 SQL Server 连接字符串的数据源属性

标签 c# .net unit-testing datasource

我正在尝试使用 DataSource 属性创建数据源,如下例所示:

[TestMethod]
[DataSource("System.Data.SqlClient", "Data Source=.\\153.71.88.80;Database=LoadData_For_R10Core_10_5_Final_Extended;Integrated Security=True;Connect Timeout=30;User Instance=True", "Products", DataAccessMethod.Sequential)]
public void GetProducts()
{
}

但我一直收到错误:

The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see "Troubleshooting Data-Driven Unit Tests" (http://go.microsoft.com/fwlink/?LinkId=62412) in the MSDN Library.

为了能够连接到 SQL 服务器,创建连接字符串的正确格式是什么?另外,我在那台机器上有 ODBC 连接,所以如果有一种方法可以使用 ODBC 连接,我会更喜欢它。

最佳答案

假设您使用的是 SQL Server 的默认实例,DataSource 应该是 153.71.88.80 , 不是都。另外,删除分隔它们的 \\

如果您使用 IP 或主机名,请确保:

  • 在 SQL Server 的配置中启用了 TCP/IP。
  • SQL Server 的 TCP 端口 1433(或您可能设置的非默认端口)未被防火墙阻止。

如果目标 SQL Server 是本地的,我将只使用 - 更少的麻烦。

关于c# - 带有 SQL Server 连接字符串的数据源属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22130157/

相关文章:

c++ - 使用 QTestLib 测试文件的打开

c# - ORM市场分析

c# - 在 Try/Catch block 中使用 Response.Redirect(url)

.net - TestDriven.Net 找不到测试

android - 如何正确测试appwidget?

unit-testing - Grails单元使用 session 对象测试功能

c# - 从 AppDomain.AssemblyLoad 事件中抛出异常

c# - Azure Web 应用程序。免费比基本版和标准版更快?

c# - 使用 IDictionary<TKey,TValue> 映射与 FluentNHibernate 的三元关联

c# - 安装不带 InstallUtil.exe 的 .NET Windows 服务