2013-02-16 349 views
0

我下載了SQL Server 2012並創建了一個名爲'Test'的本地數據庫。 然後我試圖通過我的C#代碼來訪問它在VS這樣的:用戶登錄失敗

class sqlTOdataTable 
{ 
    DataTable IndexWordsTable; 
    SqlDataAdapter sqlAdapter; 
    private string connectionString = @"Server=localhost\SQLExpress;Database=Test"; 

    public sqlTOdataTable(string TableName) 
    { 
     using (SqlConnection connection = new SqlConnection(connectionString)) 
     { 
      connection.Open(); 
      Console.WriteLine("Enter command"); 
      SqlCommand cmd = new SqlCommand(Console.ReadLine(),connection); 
      SqlDataReader Reader = cmd.ExecuteReader(); 

     } 
    } 

} 

一旦進入「connection.Open();」出現「登錄失敗的用戶」錯誤。 連接字符串有問題嗎?

回答

1

如果您想與訪問您的登錄帳戶,嘗試添加:

Trusted_Connection=True; 

如果沒有,你需要指定你的用戶名和密碼在SSMS創建:

User Id=myUsername;Password=myPassword; 
0

添加

Trusted_Connection=True; 

,所以你的ConnectionString會

private string connectionString = @"Server=localhost\SQLExpress;Database=Test;Trusted_Connection=True;"; 

www.connectionstrings.com

這假定您已經安裝的SqlServer Express作爲Authentication Mode在Windows模式下使用。這樣你的SqlExpress接受你的Windows用戶憑證。

0

如果您的服務器實例允許使用混合模式身份驗證,則必須在連接字符串中提供SQL Server登錄名/密碼。如果只允許Windows Mode,則必須使用Windows憑據登錄。

1

嘗試這種方式

string connectionString = @"Data Source=.\SQLExpress;Initial Catalog=test;Integrated Security=True";