虽然我使用ADO.NET建立了与SQL Server的连接,但它显示错误。当我使用ADO.NET连接到数据库时,出现错误
以下是代码:
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=abdul;Integrated Security=true");
SqlCommand cmd = new SqlCommand();
con.Open();
String str="select * from emp where empname='Abdul'";
cmd = new SqlCommand(str, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr == null || !dr.HasRows)
{
MessageBox.Show("No Records found");
}
else
{
while (dr.Read())
{
textBox1.Text = dr[0].ToString();
textBox2.Text = dr[1].ToString();
}
}
当我运行项目它显示了以下错误:
Cannot open database "abdul" requested by the login. The login failed.
什么要做的?
您是否创建了名为'abdul'的数据库?您的应用程序是否使用创建数据库的相同用户帐户运行? – Yahia
缺少连接字符串中的密码?尝试一下。 –
程序在其下运行的帐户是否具有服务器和数据库的权限? – Tim