此 C# 代码片段用于连接到 SQL Server 2008 实例(默认,未命名实例):
using (SqlConnection conn = new SqlConnection(sqlConnType))
{
conn.Open();
using (SqlCommand query = conn.CreateCommand())
{
query.CommandText = ConfigurationManager.AppSettings["query"].ToString();
using (SqlDataReader dataReader = query.ExecuteReader())
{
if (dataReader.HasRows && dataReader.Read())
{
// do something with the result
}
}
}
}
基于命令行参数,sqlConnType
(用于实例化 SqlConnection 对象的参数)是以下之一:
集成 Windows 身份验证:Data Source=<ServerName>;database=<DatabaseName>;Integrated Security=True
SQL 身份验证:server=<ServerName>;database=<DatabaseName>;user id=<UserID>;pwd=<Password>
我添加了防火墙规则以允许 SQL 流量。已启用命名管道和 TCP/IP。还启用了 Windows 和 SQL Server 身份验证。现在奇怪的部分......
当使用上述代码的应用程序使用 Int 远程连接到 SQL Server 时。Windows 身份验证,连接工作正常。但是,当我尝试使用 SQL Server 身份验证进行远程连接时,出现错误:Cannot open database "<DatabaseName>" requested by the login. The login failed. Login failed for user '<UserID>'.
如果我打印异常的堆栈跟踪,我会得到如下信息:
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at App.Program.Main(String[] args) in C:\App\Program.cs:line 31
注意:如果我在 SQL Server 上本地运行应用程序,则 Int. Windows 身份验证有效,但 SQL 登录身份验证再次无效。相同的应用程序可以在两种身份验证模式下使用相同的代码和相同的连接字符串与旧版本的 SQL Server(例如 2005)对话。
我究竟做错了什么?它是我缺少的特定于 SQL Server 2008 的东西吗?