c#

 一个winform界面,上面输入姓名,或者学号,点击查询时就会在下面DataGridview中显示数据,因为where条件没写,所以查询的是所有记录
请问怎样写代码当我输入学号或者姓名时查询出对应的一条数据,而不是整个数据,下面是我写的一段,请教各位
if(textBox1.Text.Trim()!=""&&textBox1.Text.Trim()!=null)
            {
                if (sql != null)
                {
                    sql +="and s.Stu_Name='"+textBox1.Text.Trim()+"'";
                }
                else
                {
                    sql += "s.Stu_Name='" + textBox1.Text.Trim() + "'";
                }    
            }
            if (textBox2.Text.Trim() != "" && textBox2.Text.Trim() != null)
            {
                if (sql != null)
                {
                    sql += "and s.Stu_ID='" + textBox2.Text.Trim() + "'";
                }
                else
                {
                    sql += "s.Stu_ID='" + textBox2.Text.Trim() + "'";
                }
            }

解决方案 »

  1.   

    select 后加 top 1 
      

  2.   

    select * from 表 where 名字='小明'
    select * from 表 where 学号='123
      

  3.   

    select * from 表 where 名字='小明' or  学号=123
    select * from 表 where 名字='小明' and 学号=123你的需求一般就是四种情况吧
      

  4.   

    select * from 表 where 学号='123
    =》
    select * from 表 where 学号=123
      

  5.   

    where后面的条件总不能我要查询某一个改一下条件吧,多麻烦啊
      

  6.   

    你写的好乱啊  帮你整理下代码            string sql = "";
                if (textBox1.Text.Trim() != "" && textBox2.Text.Trim() =="")
                {
                    if (sql != null)
                    {
                        sql += "and s.Stu_Name='" + textBox1.Text.Trim() + "'";
                    }
                }
                if (textBox2.Text.Trim() != "" && textBox1.Text.Trim() == "")
                {
                    if (sql != null)
                    {
                        sql += "and s.Stu_ID='" + textBox2.Text.Trim() + "'";
                    }
                }
                else if (textBox2.Text.Trim() != ""  &&  textBox2.Text.Trim() != "")
                {
                    if (sql != null)
                    {
                        sql += "and s.Stu_ID='" + textBox2.Text.Trim() + "' and s.Stu_Name=" + textBox1.Text.Trim();
                    }
                }
      

  7.   


                    if (sql != null)
                    {
                        sql += "and s.Stu_Name='" + textBox1.Text.Trim() + "'";
                    }
                }
                if (textBox2.Text.Trim() != "" && textBox1.Text.Trim() == "")
                {
                    if (sql != null)
                    {
                        sql += "and s.Stu_ID='" + textBox2.Text.Trim() + "'";
                    }
                }
                else if (textBox2.Text.Trim() != ""  &&  textBox2.Text.Trim() != "")
                {
                    if (sql != null)
                    {
                        sql += "and s.Stu_ID='" + textBox2.Text.Trim() + "' and s.Stu_Name=" + textBox1.Text.Trim();
                    }
                }
      

  8.   

    发错了
                string sql = "";
                if (textBox1.Text.Trim() != "" && textBox2.Text.Trim() =="")
                {
                    if (sql != null)
                    {
                        sql += "and s.Stu_Name='" + textBox1.Text.Trim() + "'";
                    }
                }
                if (textBox2.Text.Trim() != "" && textBox1.Text.Trim() == "")
                {
                    if (sql != null)
                    {
                        sql += "and s.Stu_ID='" + textBox2.Text.Trim() + "'";
                    }
                }
                else if (textBox2.Text.Trim() != ""  &&  textBox2.Text.Trim() != "")
                {
                    if (sql != null)
                    {
                        sql += "and s.Stu_ID='" + textBox2.Text.Trim() + "' and s.Stu_Name=" + textBox1.Text.Trim();
                    }
                }
      

  9.   

    你设个断点 看sql语句调试看看吧
      

  10.   

    这个确实是sql语句的问题,试着用字符串拼接的方法,吧参数传进sql语句,不会很难的
      

  11.   

     private void button1_Click(object sender, EventArgs e)
            {
                
                string sql = "";
                if (textBox1.Text.Trim() != "" && textBox2.Text.Trim() == "")
                {
                    if (sql != null)
                    {
                        sql += "and s.Stu_Name='" + textBox1.Text.Trim() + "'";
                    }
                }
                if (textBox2.Text.Trim() != "" && textBox1.Text.Trim() == "")
                {
                    if (sql != null)
                    {
                        sql += "and s.Stu_ID='" + textBox2.Text.Trim() + "'";
                    }
                }
                else if (textBox2.Text.Trim() != "" && textBox2.Text.Trim() != "")
                {
                    if (sql != null)
                    {
                        sql += "and s.Stu_ID='" + textBox2.Text.Trim() + "' and s.Stu_Name=" + textBox1.Text.Trim();
                    }
                }            ds = null;
                            sqlconn.SQL = "select p.ID as ID,Stu_Name,s.Stu_ID as Stu_ID ,p1.Pay_Name as Pay_Name ,c.Code_Name as Code_Name ,p.Res as Res  from Pay p left join  Student_Info s on p.Stu_ID=s.Stu_ID left join  Pay_Info p1 on p.Pay_ID=p1.Pay_ID left join  CodeMaster c on p.State=c.Code_No and c.Code_Type='102'";
                ds = sqlconn.ReturnDataSet();
                dataGridView1.DataSource = ds.Tables[0];
            }
      

  12.   

    你拼接后的sql在最后也没用啊 这样下就行了        private void button1_Click(object sender, EventArgs e)
            {
                string sql = "";
                if (textBox1.Text.Trim() != "" && textBox2.Text.Trim() == "")
                {
                    if (sql != null)
                    {
                        sql += " s.Stu_Name='" + textBox1.Text.Trim() + "'";
                    }
                }
                if (textBox2.Text.Trim() != "" && textBox1.Text.Trim() == "")
                {
                    if (sql != null)
                    {
                        sql += " s.Stu_ID='" + textBox2.Text.Trim() + "'";
                    }
                }
                else if (textBox2.Text.Trim() != "" && textBox2.Text.Trim() != "")
                {
                    if (sql != null)
                    {
                        sql += " s.Stu_ID='" + textBox2.Text.Trim() + "' and s.Stu_Name=" + textBox1.Text.Trim();
                    }
                }
                  
                ds = null;
                sqlconn.SQL = "select p.ID as ID,Stu_Name,s.Stu_ID as Stu_ID ,p1.Pay_Name as Pay_Name ,c.Code_Name as Code_Name ,p.Res as Res from Pay p left join Student_Info s on p.Stu_ID=s.Stu_ID left join Pay_Info p1 on p.Pay_ID=p1.Pay_ID left join CodeMaster c on p.State=c.Code_No and c.Code_Type='102' where " +sql;
                ds = sqlconn.ReturnDataSet();
                dataGridView1.DataSource = ds.Tables[0];        }
      

  13.   

    第三个else if写错了 应该是->
           else if (textBox1.Text.Trim() != "" && textBox2.Text.Trim() != "")
                {
                    if (sql != null)
                    {
                        sql += " s.Stu_ID='" + textBox2.Text.Trim() + "' and s.Stu_Name=" + textBox1.Text.Trim();
                    }
                }