Form上3个Text框,分别代表 姓名,班级,系需要随便输入一个,或者其中几个,然后按下搜索按钮后,在下方的 datagridview中显示搜索到的结果。我对数据库的操作不太懂,请问如何实现,我现在已经可以生成一个string,里面是完整的sql查询语句 select name,class,dept from student,class,dept where name like ‘%name%’ and…… 这种但是我不知道如何将语句发送给sql server,再如何将返回的数据显示再datagridview里。假设 数据库再本地,登陆形式为windows用户登陆。数据库名为 school。

解决方案 »

  1.   

     strSql="放你的sql查询语句";
     sqlcn = new MySqlConnection(); 
     sqlcn.ConnectionString ="server=.;database=nsytowel;Integrated Security=SSPI";
     sqlcn.Open();
     sqlda = new MySqlDataAdapter(strSql, sqlcn);
     ds = new DataSet();
     sqlda.Fill(ds);
     Close();
     datagridview.datasource=ds.table[0].DefaultView;
      

  2.   


    SqlConnection conn = new SqlConnection("server=(local);database=school;Integrated Security=SSPI");
    SqlCommand cmd = new SqlCommand("你的sql语句",conn);
    SqlDataAdapter sda = new SqlAdapter(cmd);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    dataGridView1.DataSource = dt;
      

  3.   


    姓名,班级,系
    STRING sql="select * from table where 1=1 "
    if( txtname.text!="")
    {
    sql+=" and  name=txtname.text.replace("'","''") "
    }
    if( txtclasses.text!="")
    {
    sql+=" and  name=txtclasses.text.replace("'","''") "
    }
    if( txtxi.text!="")
    {
    sql+=" and  name=txtxi.text.replace("'","''") "}
    sql+=" order by name desc "SqlConnection conn = new SqlConnection("server=(local);database=school;Integrated Security=SSPI");
    SqlCommand cmd = new SqlCommand("sql",conn);
    SqlDataAdapter sda = new SqlAdapter(cmd);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    dataGridView1.DataSource = dt;
      

  4.   


    姓名 name班级 classes,系 xi
    STRING sql="select * from table where 1=1 "
    if( txtname.text!="")
    {
    sql+=" and  name=txtname.text.replace("'","''") "
    }
    if( txtclasses.text!="")
    {
    sql+=" and  classes=txtclasses.text.replace("'","''") "
    }
    if( txtxi.text!="")
    {
    sql+=" and  xi=txtxi.text.replace("'","''") "}
    sql+=" order by name desc "SqlConnection conn = new SqlConnection("server=(local);database=school;Integrated Security=SSPI");
    SqlCommand cmd = new SqlCommand("sql",conn);
    SqlDataAdapter sda = new SqlAdapter(cmd);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    dataGridView1.DataSource = dt;