0

我正在尝试连接到 SQL Server。使用这段代码

public static String getDBConnection()
    {
        SqlConnectionStringBuilder csBuilder = new SqlConnectionStringBuilder();

        csBuilder.InitialCatalog = "AdDatabase";
        csBuilder.DataSource = "JAWADAMJAD-PC\\SQLEXPRESS";
        csBuilder.UserID = "Jawad";
        csBuilder.Password = "123456";

         return csBuilder.ConnectionString;
    }

我已经通过 SQL Server 管理控制台添加了这个用户名。我仍然无法弄清楚为什么我无法连接到 SQL Server。我必须做其他任何事情才能成功连接吗?如果是这样,那是什么?请帮忙!!!我正在使用 Visual Studio 2010 和 SQL Server 2008。

我收到以下异常

    System.Data.SqlClient.SqlException: Login failed for user 'Jawad'.
   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 AdServiceNew.Service1.initDB() in C:\Users\OZI TECH\Desktop\AdServiceNew\AdServiceNew\AdServiceNew\Service1.asmx.cs:line 57

我去了服务器日志,它说

An attempt to login as SQL Authentication Failed. Server is configured for windows authentication only.
4

1 回答 1

1

如果您只有 Windows 身份验证模式,则需要将 Integrated Security 选项设置为 true 并去掉用户和密码。

System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(); builder["数据源"] = "(本地)"; builder["集成安全"] = true; builder["初始目录"] = "AdventureWorks;NewValue=Bad"; Console.WriteLine(builder.ConnectionString);

有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx

于 2013-05-22T14:02:55.193 回答