string connection = "server=.;database=mydb;user id=sa;pwd=123";
            SqlConnection myconnection = new SqlConnection(connection);
            SqlDataAdapter sql = new SqlDataAdapter();
            sql.SelectCommand.CommandText = "Select 学生号,姓名,年龄,性别 from 学生表"; 
            DataSet set = new DataSet();
            sql.Fill(set, "学生表");
            dataGridView1.DataSource = set.Tables["学生表"];

解决方案 »

  1.   

    sql.SelectCommand.CommandText = "Select 学生号,姓名,年龄,性别 from 学生表";  改为sql.SelectCommand = new SqlCommand("Select 学生号,姓名,年龄,性别 from 学生表", myconnection);
      

  2.   

    dataGridView1.DataSource = set.Tables["学生表"];
    下断点,看set.Tables["学生表"]是否存在。
      

  3.   


    string s = textBox1.Text; string connection = "server=.;database=mydb;user id=sa;pwd=123";
                        SqlConnection myconnection = new SqlConnection(connection);
                        SqlDataAdapter sql = new SqlDataAdapter();
                        sql.SelectCommand = new SqlCommand("Select 学生号,姓名,年龄,性别 from 学生表 where 学生号='" + s + "'", myconnection);
                        n = sql.Fill(set, "学生表");
                        dataGridView1.DataSource = set.Tables["学生表"];
                        if (n == 0)
                        { MessageBox.Show("输入有误", "请重新输入"); }为什么会执行 MessageBox.Show("输入有误", "请重新输入");?
    其他链接都对了~
      

  4.   

    string s = textBox1.Text; string connection = "server=.;database=mydb;user id=sa;pwd=123";
      SqlConnection myconnection = new SqlConnection(connection);
      SqlDataAdapter sql = new SqlDataAdapter();
      sql.SelectCommand = new SqlCommand("Select 学生号,姓名,年龄,性别 from 学生表 where 学生号='" + s + "'", myconnection);
      n = sql.Fill(set, "学生表");
      dataGridView1.DataSource = set.Tables["学生表"];
      if (n == 0)
      { MessageBox.Show("输入有误", "请重新输入"); }为什么在dataGridView1 显示不出?
    其他链接都对了~
      

  5.   

     1. SqlDataAdapter sql = new SqlDataAdapter();这样的命名看起来很别扭,你可以用Adapter,Da什么的。2.get,set是系统关键字,你用来作Dataset,这样的命名更加不规范, 而且没有 Dataset set =new Dataset();3. sql.SelectCommand = new SqlCommand("Select 学生号,姓名,年龄,性别 from 学生表 where 学生号='" + s + "'", myconnection);这句可分为两句写,看起更直观
    string sqlstr="Select 学生号,姓名,年龄,性别 from 学生表 where 学生号='" + s + "'";
    sql.SelectCommand = new SqlCommand(sqlstr,myconnection);
      

  6.   

    dataGridView1.DataSource = set.Tables["学生表"];
    这个下面加一句
    dataGridView1.DataBind();
      

  7.   

      dataGridView1.DataSource = set.Tables["学生表"];
    下面加一句
    dataGridView1.DataBind();
      

  8.   


    string connection = "server=.;database=mydb;user id=sa;pwd=123";
      SqlConnection myconnection = new SqlConnection(connection);     
      string sql = "Select 学生号,姓名,年龄,性别 from 学生表"; 
      SqlDataAdapter sql = new SqlDataAdapter(sql,con); 
      DataSet set = new DataSet();
      sql.Fill(set, "学生表");
      dataGridView1.DataSource = set.Tables["学生表"];
      

  9.   

    修改下  string connection = "server=.;database=mydb;user id=sa;pwd=123";
      SqlConnection myconnection = new SqlConnection(connection);  
      con.open();   
      string sql = "Select 学生号,姓名,年龄,性别 from 学生表"; 
      SqlDataAdapter sda = new SqlDataAdapter(sql,con); 
      DataSet ds= new DataSet();
      sda.Fill(ds, "学生表");
      dataGridView1.DataSource = ds.Tables["学生表"];
      

  10.   


    string s = textBox1.Text; string connection = "server=.;database=mydb;user id=sa;pwd=123";
      SqlConnection myconnection = new SqlConnection(connection);
      SqlDataAdapter sql = new SqlDataAdapter();
      sql.SelectCommand = new SqlCommand("Select 学生号,姓名,年龄,性别 from 学生表 where 学生号='" + s + "'", myconnection);
      n = sql.Fill(set, "学生表");
    if(set.Tables["学生表"].Rows.Count < 1)
    {
     MessageBox.Show("木有数据"); 
     return;
    }
      dataGridView1.DataSource = set.Tables["学生表"];
      if (n == 0)
      { MessageBox.Show("输入有误", "请重新输入"); }
      

  11.   

    delete from 学生表 where 学生号='" + textBox7.Text + "' or 姓名= '" + textBox8.Text + "' or 年龄='" + textBox9.Text + "' or 性别='" + textBox10.Text + "'请问下在C#下只能输入学生号能输出,而输入其他为什么不能删除~
    谢谢楼上几位~
      

  12.   

    你别这样。你这样
    StringBuilder sb=new StringBuilder();string sql =@"delete  from 学生表 where 1=1  ";
    if(textBox7.Text!="")
    sb.Append("and 学生号='" + textBox7.Text + "'");
    if(textBox8.Text!="")
    sb.Append("and 姓名='" + textBox8.Text + "'");
    if(textBox9.Text!="")
    sb.Append("and 年龄='" + textBox9.Text + "'");
    if(textBox10.Text!="")
    sb.Append("and 性别='" + textBox10.Text + "'");
    sql=sql+sb.ToString();