private void button1_Click(object sender, EventArgs e)
{
// 建立连接字符串
string strconnection = "user id=sa; password=jian0725;";
strconnection += "initial catalog=Northwind;server=baserker;";
strconnection += "connect timeout=30;";
SqlConnection mylink = new SqlConnection(strconnection);
mylink.Open();
SqlDataAdapter myadapter = new SqlDataAdapter();
DataSet mydataset = new DataSet();
string strcom = "select employeeid,lastname,firstname from employees order by employeeid";
myadapter.SelectCommand = new SqlCommand(strcom, mylink);
SqlCommandBuilder mycb = new SqlCommandBuilder(myadapter);
myadapter.Fill(mydataset, "employees");
dataGridView1.DataSource = mydataset;
mylink.Close();
}
{
// 建立连接字符串
string strconnection = "user id=sa; password=jian0725;";
strconnection += "initial catalog=Northwind;server=baserker;";
strconnection += "connect timeout=30;";
SqlConnection mylink = new SqlConnection(strconnection);
mylink.Open();
SqlDataAdapter myadapter = new SqlDataAdapter();
DataSet mydataset = new DataSet();
string strcom = "select employeeid,lastname,firstname from employees order by employeeid";
myadapter.SelectCommand = new SqlCommand(strcom, mylink);
SqlCommandBuilder mycb = new SqlCommandBuilder(myadapter);
myadapter.Fill(mydataset, "employees");
dataGridView1.DataSource = mydataset;
mylink.Close();
}
这样是不会显示的,datagridview里要显示的是表.你改成
dataGridView1.DataSource = mydataset.Tables[0];