一个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() + "'";
}
}
请问怎样写代码当我输入学号或者姓名时查询出对应的一条数据,而不是整个数据,下面是我写的一段,请教各位
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() + "'";
}
}
select * from 表 where 学号='123
select * from 表 where 名字='小明' and 学号=123你的需求一般就是四种情况吧
=》
select * from 表 where 学号=123
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();
}
}
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();
}
}
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();
}
}
{
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];
}
{
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]; }
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();
}
}