日期:2014-05-18  浏览次数:20792 次

我想用C#语句把下面的SQL查询结果显示在dataGridView里。该怎么写?求指教
我想用C#语句把下面的SQL查询结果显示在dataGridView里,请教各位下面“???”要怎么写。先拜谢了!!


  SqlConnection conn = new SqlConnection("server=.;database=student;uid=sa;pwd=123;");
  SqlCommand cmd = new SqlCommand("select * from View_results where termNo=1",conn);
  conn.Open();
  cmd.ExecuteNonQuery();
  SqlDataAdapter sda = new SqlDataAdapter(cmd);
  sda.Fill(Class_SQL.ds,"select_results");
  if (Class_SQL.ds.Tables["select_results"].Rows.Count == 1)
  {
  ???
  }

------解决方案--------------------
cmd.ExecuteNonQuery();//去掉该句

......
sda.Fill(Class_SQL.ds,"select_results");
conn.Close();
dataGridView1.DataSource=Class_SQL.ds.Tables["select_results"];//关键一句

------解决方案--------------------
断开式链接这样些

C# code
 SqlConnection conn = new SqlConnection("server=.;database=student;uid=sa;pwd=123;");
  SqlCommand cmd = new SqlCommand("select * from View_results where termNo=1",conn);
  SqlDataAdapter sda = new SqlDataAdapter(cmd);
  sda.Fill(Class_SQL.ds,"select_results");
  if (Class_SQL.ds.Tables["select_results"].Rows.Count == 1)
  {
     dataGridView1.DataSource=Class_SQL.ds.Tables["select_results"].DefaultView;
     //dataGridView1.DataSource=Class_SQL.ds.Tables["select_results"];     
     //dataGridView1.DataSource=Class_SQL.ds;
  }