今天我用visual studio 2017的c#应用窗体程序打开我在sql server 2008里建的数据库。
private void Form1_Load(object sender, EventArgs e)
{
//创建数据库连接字符串
string SqlStr = "Server=DESKTOP-KPKB4AA;DataBase=Factory;User Id=admin;Pwd=;Integrated Security=True";
// string SqlStr = "Data Source=DESKTOP-KPKB4AA;Initial Catalog=Factory;Integrated Security=True";
// string SqlStr = "Data Source=DESKTOP-KPKB4AA;Initial Catalog=Factory;User Id=sa;Pwd=;Integrated Security=True";
SqlConnection con = new SqlConnection(SqlStr);
con.Open();
if (con.State == ConnectionState.Open)
{
label1.Text = "SQL Server数据库连接开启";
con.Close();
}
if (con.State == ConnectionState.Closed)
{
label2.Text = "SQL Server数据库连接关闭";
}
}
加粗的部分都是正确的,出现标题的错误是因为缺少Integrated Security=True。