用户

时间:2018-12-28 12:18:07

标签: c# sql-server database

我知道这个问题会不时地重复,但是我在这里尝试了几乎所有答复,但是仍然遇到下面的代码尝试连接到我的MS SQL Server 2014的问题:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "(LocalDB)\\v11.0";
builder.UserID = "LOGIN";
builder.Password = "PASSWORD";
builder.InitialCatalog = "DATABASE";
builder.IntegratedSecurity = false;
SqlConnection connection = new SqlConnection(builder.ConnectionString);
connection.Open();

我用密码PASSWORD创建了一个用户登录:

CREATE LOGIN [LOGIN] WITH
PASSWORD=N'PASSWORD',
DEFAULT_DATABASE=[DATABASE],
DEFAULT_LANGUAGE=[us_english],
CHECK_EXPIRATION=OFF,
CHECK_POLICY=ON

并启用帐户。

alter login LOGIN enable

我检查了我的Microsoft SQL Server 2014 Management Studio用户LOGIN是否在Security文件夹中,并且在Database Secuirty Users文件夹中是否还包含用户LOGIN。

我也尝试在Visual Studio中使用Windows身份验证连接数据库,并成功。但是,即使尝试执行以下代码,我也无法使用上面的代码重复此连接过程:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "(LocalDB)\\v11.0";
builder.InitialCatalog = "DATABASE";
builder.IntegratedSecurity = true;
SqlConnection connection = new SqlConnection(builder.ConnectionString);
connection.Open();

我收到以下错误:

System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'LOGIN'.
    in System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)
    in System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
    in System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
    in System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
    in System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
    in System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
    in System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
    in System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
    in System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
    in System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
    in System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
    in System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
    in System.Data.SqlClient.SqlConnection.Open()

此数据库是我的服务器上的新数据库;我昨天进行了新安装,据我所知,我使用默认安装设置。

2 个答案:

答案 0 :(得分:0)

SQL Server中的身份验证类型很少,例如窗口,服务器等。

您通过SQL创建的登录名将尝试通过SQL Server身份验证模式登录。因此,请确保您的SQL Server允许使用该类型的身份验证。

您可以检查SQL Server设置:

右键单击服务器名称->属性->安全性:

混合模式身份验证应同时允许Windows和服务器:

enter image description here

答案 1 :(得分:0)

感谢您的回答。解决方案在下面的代码中:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = ".";            // DataSource changed             
builder.InitialCatalog = "DATABASE";
builder.IntegratedSecurity = true;
SqlConnection connection = new SqlConnection(builder.ConnectionString);
connection.Open();