2013-10-31 136 views
0

應用我開發在asp.net應用程序,並遇到了以下問題:用戶登錄失敗在ASP.NET

protected void Button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     string connString = "Data Source = JOHN-4E29FADEE; database=PEOPLE;"; 
     SqlConnection conn = new SqlConnection(connString); 
     conn.Open(); 
     Response.Write("Succes"); 
     SqlCommand comanda = new SqlCommand(); 
     comanda.Connection = conn; 
     comanda.CommandType = System.Data.CommandType.Text; 
     comanda.CommandText = "Select PERSON_ID,FIRST_NAME,FATHER_INITIALS, LAST_NAME, BIRTHDATE,CNP,MOTHER_CNP,FATHER_CNP from PERSON order by FIRST_NAME"; 
     SqlDataReader dr = comanda.ExecuteReader(); 
     GridView4.DataSource = dr; 
     GridView4.DataBind(); 
     conn.Close(); 
    } 
    catch (SqlException exc) 
    { 
     Response.Write("Connection Error! " + exc.Message); 
    } 
} 

當我啓動應用程序時,我得到Connection Error! Login failed for user

回答

2

您需要在連接字符串中提供sqlserver數據庫的用戶名和密碼。

+0

我連接到SQL Server使用Windows身份驗證和我不在我安裝SQL Server 2008時,請記得爲SQL Server身份驗證設置用戶名和密碼。我應該在這種情況下執行什麼操作? –

+0

你必須使用你的Windows憑證。用戶名和密碼您提供登錄 –

+0

我提供的憑據,但唯一的變化是,連接錯誤!用戶John登錄失敗。 –

0

試試這個:

connetionString="Data Source=ServerName;Initial Catalog=Databasename; 
    User ID=UserName;Password=Password" 
+0

我連接到與Windows身份驗證的SQL服務器 –

+0

你應該試試這個: - connectionString =「Initial Catalog = MyDb; Data Source = MyServer; Integrated Security = SSPI ;」 – kaushik0033

+0

非常感謝。我試了一下,它的工作原理:) –

0

嘗試SqlConnectionStringBuilder ...更容易一些值分配給屬性,而不是從頭開始創建一個神祕的連接字符串