连接到 SQL 服务器数据库进行单元测试时 C# 登录失败
C# login failed when connecting to SQL Server database for unit testing
我正在做一个项目,当我试图 运行 单元测试来检查进度时,我收到一个错误:
Initialization method UnitTestProject.UnitTest1.Init threw exception. System.Data.SqlClient.SqlException: Cannot open database "database" requested by the login. The login failed.
Login failed for user 'User\User1'
我检查过 User1
是 SQL 服务器的所有者,我认为我的连接字符串正确,但我一定遗漏了一些明显的东西,请告诉我:)
我的存储库 class:
public class Repository
{
private string _connectionString;
public Repository()
{
_connectionString = configurationManager.ConnectionStrings["database"].ConnectionString;
}
public IEnumerable<Person> GetPeople()
{
var people = new List<Person>();
using (SqlConnection connection = new SqlConnection(_connectionString))
{
string commandText = "SELECT dbo.People.Id"
+ ", dbo.People.FirstName"
+ ", dbo.People.LastName"
+ ", dbo.People.Height"
+ ", dbo.People.Weight"
+ "FROM dbo.People";
SqlCommand command = new SqlCommand(commandText, connection);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
people.Add(new Person()
{
Id = reader.GetInt32(0),
FirstName = reader.GetString(1),
LastName = reader.GetString(2),
Height = reader.GetDouble(3),
Weight = reader.GetDouble(4)
});
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
return people;
}
}
我在 App.config
中的连接字符串:
add name="database"
connectionString="Data Source=localhost;Initial Catalog=database;Integrated Security=true"
我的单元测试:
public class UnitTest1
{
private Repository _repo;
[TestInitialize]
public void Init()
{
_repo = new Repository();
_repo.ResetDatabase();
}
[TestMethod]
public void GetPeople()
{
int count = _repo.GetPeople().Count();
Assert.IsTrue(count > 0);
}
}
尝试添加该元素以清除之前可能定义的任何连接字符串:
<connectionStrings>
<clear />
<add name="DBConnection" connectionString="Data Source=xxxxxxxxxxx\xxxxxxxxx;Initial Catalog=xxxxx;Persist Security Info=True;User ID=xxxxxxxx;Password=xxxxxxxx"
providerName="System.Data.SqlClient" />
</connectionStrings>
似乎通过 SQL 身份验证创建 SQL 服务器登录,即用户 ID 和密码让我解决了登录问题。
然后我还必须从以下位置更新连接字符串安全性:
Integrated Security=true
至:
User id=admin;Password=password
不幸的是,我现在在 运行 测试时看到这个错误:
Initialization method UnitTestProject.UnitTest1.Init threw exception.
System.Data.SqlClient.SqlException: A connection was successfully
established with the server, but then an error occurred during the
login process. (provider: Shared Memory Provider, error: 0 - No
process is on the other end of the pipe.) --->
System.ComponentModel.Win32Exception: No process is on the other end
of the pipe.
不过,我将从现有的解决方案中尝试:
No process is on the other end of the pipe (SQL Server 2012)
我正在做一个项目,当我试图 运行 单元测试来检查进度时,我收到一个错误:
Initialization method UnitTestProject.UnitTest1.Init threw exception. System.Data.SqlClient.SqlException: Cannot open database "database" requested by the login. The login failed.
Login failed for user 'User\User1'
我检查过 User1
是 SQL 服务器的所有者,我认为我的连接字符串正确,但我一定遗漏了一些明显的东西,请告诉我:)
我的存储库 class:
public class Repository
{
private string _connectionString;
public Repository()
{
_connectionString = configurationManager.ConnectionStrings["database"].ConnectionString;
}
public IEnumerable<Person> GetPeople()
{
var people = new List<Person>();
using (SqlConnection connection = new SqlConnection(_connectionString))
{
string commandText = "SELECT dbo.People.Id"
+ ", dbo.People.FirstName"
+ ", dbo.People.LastName"
+ ", dbo.People.Height"
+ ", dbo.People.Weight"
+ "FROM dbo.People";
SqlCommand command = new SqlCommand(commandText, connection);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
people.Add(new Person()
{
Id = reader.GetInt32(0),
FirstName = reader.GetString(1),
LastName = reader.GetString(2),
Height = reader.GetDouble(3),
Weight = reader.GetDouble(4)
});
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
return people;
}
}
我在 App.config
中的连接字符串:
add name="database"
connectionString="Data Source=localhost;Initial Catalog=database;Integrated Security=true"
我的单元测试:
public class UnitTest1
{
private Repository _repo;
[TestInitialize]
public void Init()
{
_repo = new Repository();
_repo.ResetDatabase();
}
[TestMethod]
public void GetPeople()
{
int count = _repo.GetPeople().Count();
Assert.IsTrue(count > 0);
}
}
尝试添加该元素以清除之前可能定义的任何连接字符串:
<connectionStrings>
<clear />
<add name="DBConnection" connectionString="Data Source=xxxxxxxxxxx\xxxxxxxxx;Initial Catalog=xxxxx;Persist Security Info=True;User ID=xxxxxxxx;Password=xxxxxxxx"
providerName="System.Data.SqlClient" />
</connectionStrings>
似乎通过 SQL 身份验证创建 SQL 服务器登录,即用户 ID 和密码让我解决了登录问题。
然后我还必须从以下位置更新连接字符串安全性:
Integrated Security=true
至:
User id=admin;Password=password
不幸的是,我现在在 运行 测试时看到这个错误:
Initialization method UnitTestProject.UnitTest1.Init threw exception. System.Data.SqlClient.SqlException: A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.) ---> System.ComponentModel.Win32Exception: No process is on the other end of the pipe.
不过,我将从现有的解决方案中尝试:
No process is on the other end of the pipe (SQL Server 2012)