string cmd = "select * from mydb";
SqlCommand sql = new SqlCommand(cmd);
SqlConnection conn = new SqlConnection(@"Data Source=Localhost;Database=company;Integrated Security=True");
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = sql;
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
da.Fill(ds);這裡提示錯誤:Fill: SelectCommand.Connection 屬性尚未初始化。
da.SelectCommand = sql;
da.SelectCommand.Connection=conn; //加上這個