2017-10-11 143 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 
} 
相关问题