在WINFORM窗体中,有这么几个控件:checkbox1, checkbox2,checkbox3 textbox1,textbox2,textbox3,
button1, datagridview1 表名是 student 有三个字段 ID name telephone
当点击button1时,根据textbox中的输入进行查询
在button1中的代码如下(直接打的,没有在VC里写):
string aa="",bb="",cc="";
if (checkbox1.checked==true)
{ aa= textbox1.text;}
else
{ aa="ID";}
if (checkbox2.checked==true)
{ bb= textbox2.text;}
else
{ bb="name";}
if (checkbox3.checked==true)
{ cc= textbox3.text;}
else
{ cc="telephone";}
string strsel="select * from student where ID="+aa+" and name="+bb+" and telephone="+cc+"";datafridview1.datasource=connection.dset(strsel).table[0];说明下,“connection”是我写的一个方法类,贴出来吧:
class connection
{
public static DataSet dtset(string str)
{ string strsqlcn = "Initial Catalog=school;Data Source=(local);Integrated Security=SSPI;";
SqlConnection cn = new SqlConnection(strsqlcn);
SqlDataAdapter adp = new SqlDataAdapter(str, cn);
DataSet dset = new DataSet();
adp.Fill(dset);
return dset;
}
}
个人认为问题的关键在SQL 语句,当checkbox都为FALSE 或第一个为true时,是没有问题,如果第一个checked为false ,就有问题了
还请高人指点下
另外说明下,此处主要用在对表的多条件混合查询中,用户可以自由选择查询条件,本来是要用IF {if {if{}else} else}ELSE
这种方式嵌套的,但是感觉不太好,所有就想了这么一段代码来实现目的
button1, datagridview1 表名是 student 有三个字段 ID name telephone
当点击button1时,根据textbox中的输入进行查询
在button1中的代码如下(直接打的,没有在VC里写):
string aa="",bb="",cc="";
if (checkbox1.checked==true)
{ aa= textbox1.text;}
else
{ aa="ID";}
if (checkbox2.checked==true)
{ bb= textbox2.text;}
else
{ bb="name";}
if (checkbox3.checked==true)
{ cc= textbox3.text;}
else
{ cc="telephone";}
string strsel="select * from student where ID="+aa+" and name="+bb+" and telephone="+cc+"";datafridview1.datasource=connection.dset(strsel).table[0];说明下,“connection”是我写的一个方法类,贴出来吧:
class connection
{
public static DataSet dtset(string str)
{ string strsqlcn = "Initial Catalog=school;Data Source=(local);Integrated Security=SSPI;";
SqlConnection cn = new SqlConnection(strsqlcn);
SqlDataAdapter adp = new SqlDataAdapter(str, cn);
DataSet dset = new DataSet();
adp.Fill(dset);
return dset;
}
}
个人认为问题的关键在SQL 语句,当checkbox都为FALSE 或第一个为true时,是没有问题,如果第一个checked为false ,就有问题了
还请高人指点下
另外说明下,此处主要用在对表的多条件混合查询中,用户可以自由选择查询条件,本来是要用IF {if {if{}else} else}ELSE
这种方式嵌套的,但是感觉不太好,所有就想了这么一段代码来实现目的
string strsel="select * from student where ID='"+aa+"' and name='"+bb+"' and telephone='"+cc+"'";
但是要考虑一个问题,如果CHECKBOX.CHECKED==FALSE 那么aa= ID ,加了单引号,那就查询不到数据了 在程序运行中 string strsel="select * from student where ID="+aa+"";
这个样子也是可以执行的 不管CHECKBOX.CHECKED==FALSE 还是TRUE 程序都是可以执行的
这里不可以再做判断是获取的TEXT的值 还是直接赋予的值,
否则就进入了IF ELSE 嵌套了
------------------------create procedure
要不就写个SQL生成语句,这个比较麻烦。要不你就多写几个判断根据判断TEXT为空重新写SQL语句。
if (textbox1.text.length>0)
{aa= textbox1.text;}
else
{messagebox.show("内容不能为空","提示")}
也是一样的问题,
我进入SQL查询分析器看过了 select * from student where ID=ID and name=name and telephone='1234567'没有什么问题
但是在VC里无法执行
if (checkbox1.checked==FALSE)
{ aa="ID";}
select 语句好像就变成 select * from student where ID='ID' 了
查询不到数据了 不过我还是再查询下‘’吧
if (checkbox1.checked==true)
{ aa= textbox1.text;}
else
{ aa="ID";}
if (checkbox2.checked==true)
{ bb= "'"+textbox2.text+"'";}
else
{ bb="name";}
if (checkbox3.checked==true)
{ cc= "'"+textbox3.text+"'";}
else
{ cc="telephone";}
string strsel="select * from student where ID="+aa+" and name="+bb+" and telephone="+cc+"";
这样不就可以了?虽然用三目运算符加String.Format方法会更简洁些,不过你先这样凑合着用吧。