protected string connStr = "Data Source=.;Initial Catalog=NewDB;Integrated Security=True";
private void Form1_Load(object sender, EventArgs e)
{
string SelectSql = "Select * from news";
SqlConnection cn = new SqlConnection(connStr);
SqlDataAdapter da = new SqlDataAdapter(SelectSql, cn);
DataSet ds = new DataSet();
cn.Open(); ---------→①
da.Fill(ds);
dataGrid1.DataSource = ds;
//dataGrid1.DataBindings();
cn.Close();
}
执行到①句时就报错。具体信息是:
未处理 System.Data.SqlClient.SqlException
Message="SqlException"
Class=20
LineNumber=0
Number=17
Procedure="ConnectionOpen (Connect())."
Server="."
Source=".Net SqlClient Data Provider"
State=0
StackTrace:
位于 System.Data.SqlClient.SqlConnection.OnError()
位于 System.Data.SqlClient.SqlInternalConnection.OnError()
位于 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
位于 System.Data.SqlClient.TdsParser.Connect()
位于 System.Data.SqlClient.SqlInternalConnection.OpenAndLogin()
位于 System.Data.SqlClient.SqlInternalConnection..ctor()
位于 System.Data.SqlClient.SqlConnection.Open()
位于 ShowDB.Form1.Form1_Load()
位于 System.Windows.Forms.Form.OnLoad()
位于 System.Windows.Forms.Form._SetVisibleNotify()
位于 System.Windows.Forms.Control.set_Visible()
位于 System.Windows.Forms.Application.Run()
位于 ShowDB.Program.Main()
好像是数据库不能打开似的。怎么解决呢?
private void Form1_Load(object sender, EventArgs e)
{
string SelectSql = "Select * from news";
SqlConnection cn = new SqlConnection(connStr);
SqlDataAdapter da = new SqlDataAdapter(SelectSql, cn);
DataSet ds = new DataSet();
cn.Open(); ---------→①
da.Fill(ds);
dataGrid1.DataSource = ds;
//dataGrid1.DataBindings();
cn.Close();
}
执行到①句时就报错。具体信息是:
未处理 System.Data.SqlClient.SqlException
Message="SqlException"
Class=20
LineNumber=0
Number=17
Procedure="ConnectionOpen (Connect())."
Server="."
Source=".Net SqlClient Data Provider"
State=0
StackTrace:
位于 System.Data.SqlClient.SqlConnection.OnError()
位于 System.Data.SqlClient.SqlInternalConnection.OnError()
位于 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
位于 System.Data.SqlClient.TdsParser.Connect()
位于 System.Data.SqlClient.SqlInternalConnection.OpenAndLogin()
位于 System.Data.SqlClient.SqlInternalConnection..ctor()
位于 System.Data.SqlClient.SqlConnection.Open()
位于 ShowDB.Form1.Form1_Load()
位于 System.Windows.Forms.Form.OnLoad()
位于 System.Windows.Forms.Form._SetVisibleNotify()
位于 System.Windows.Forms.Control.set_Visible()
位于 System.Windows.Forms.Application.Run()
位于 ShowDB.Program.Main()
好像是数据库不能打开似的。怎么解决呢?