2017-10-11 172 views
-3
private void Form1_Load(object sender, EventArgs e) 
    { 
     SqlConnection baglanti = new SqlConnection(); 
     baglanti.ConnectionString = "Server=DELL\\SQLEXPRESS; Database=Ibrahimoz; Integrated Security=true; "; 
     SqlCommand komut = new SqlCommand(); 
     komut.CommandText = "select * from User"; 
     komut.Connection = baglanti; 
     baglanti.Open(); 
     SqlDataReader rdr= komut.ExecuteReader(); 

     while (rdr.Read()) 
     { 
      string adi = rdr["Name"].ToString(); 
      string familya = rdr["Surname"].ToString(); 
      listbox.Items.Add(string.Format("{0} - {1}" ,adi ,familya)); 


     } 
     baglanti.Close(); 

SQL連接和錯誤:錯誤:System.Data.SqlClient.SqlException:'無法打開數據庫「Ibrahimoz」請求登錄。登錄失敗。用戶登錄失敗

baglan.open(); cannot request database()

System.Data.SqlClient.SqlException: 'Cannot open database "Ibrahimoz" requested by the login. The login failed. Login failed for user

請幫助我。

回答

0

你確定使用了連接字符串嗎?請檢查數據庫名稱/實例名稱。 您還可以檢查連接字符串幫助:https://www.connectionstrings.com/

順便說一下,您應該使用「using」語句。有點像:

string szConnection = String.Format(@"Data Source={0};Initial Catalog=master;User ID={1};Password={2};"); 

using(SqlConnection connection = new SqlConnection(szConnection)) 
{ 
    connection.Open(); 
    // Use the connection 
} 
相關問題