查询功能:界面上有4个输入框, 每个输入框的值作为查询条件(可以为空)。
4个框的值需要关联查询5个表用的方法太笨了,请教一个简单的方法:
if (textBox1.Text.Trim() != "" && (textBox2.Text.Trim() != "" && textBox3.Text.Trim() != "" && (textBox4.Text.Trim() != "")
{sql = }
if (textBox1.Text.Trim() != "" && (textBox2.Text.Trim() != "" && textBox3.Text.Trim() != "")
{sql = }
if (textBox1.Text.Trim() != "" && (textBox2.Text.Trim() != "")
{sql = }
........................
........................
........................
.................................................
4个框的值需要关联查询5个表用的方法太笨了,请教一个简单的方法:
if (textBox1.Text.Trim() != "" && (textBox2.Text.Trim() != "" && textBox3.Text.Trim() != "" && (textBox4.Text.Trim() != "")
{sql = }
if (textBox1.Text.Trim() != "" && (textBox2.Text.Trim() != "" && textBox3.Text.Trim() != "")
{sql = }
if (textBox1.Text.Trim() != "" && (textBox2.Text.Trim() != "")
{sql = }
........................
........................
........................
.................................................
解决方案 »
- 突然有些迷茫了,大家没事讨论下。
- 根据数据库表设置的层次标记来生成具有结构的Xml文件
- C#键盘钩子为什么执行两次?
- 页面重定向之后刷新的问题
- 用HttpWebRequest抓WEB页面内容,多线程的情况下,如果有一个线程遇到一个很慢网站,其它线程的速度也会变的很慢。
- 求DataTable导出EXCEL的DEMO,谢谢
- 好像水平高的.net程序员很少?
- 如果在一张SQL表面,这张表有三列的数据,如果我要对某一列里面的值不定时的去插入数据,原先的二列数据保持不变,这要怎么办呀?
- 关于C#类型
- 请问如何像foxmail一样把IE内嵌在窗体中浏览邮件啊??急!!
- .net有没有类似于Excel的标签控件?
- 为什么我的C#2005按 Ctrl+E 加 C 不能进行多行注释?
{
sql+=" and xxx ";
}
if (textBox2.Text.Trim() != "")
{
sql+=" and xxx ";
}
...
拼接SQL语句
if (textBox1.Text.Trim() != "")
{
sql+=" and xxx ";
}
if (textBox2.Text.Trim() != "")
{
sql+=" and xxx ";
}
...
[
if (textBox1.Text.Trim() != "" ){ sql ="...." }
if (textBox2.Text.Trim() != "" )
{
if(sql=="" ) sql="....."
else sql+={.......}
}
if (textBox3.Text.Trim() != "" )
{
if(sql=="" ) sql="....."
else sql+={.......}
}
.....
.....
.....
if (textBox1.Text.Trim() != "")
{
sql+=" and a.xx";
}
if (textBox2.Text.Trim() != "")
{
sql+=" and b.xx";
}
...
if (textBox1.Text.Trim() != "")
{
sql+=" and a.xx";
}
if (textBox2.Text.Trim() != "")
{
sql+=" and b.xx";
}
...
if(!String.IsNullOrEmpty(sqlWhere))
{
sqlWhere = sqlWhere.SubString(5);
}
string strSQL = " insert ...values(....";
foreach (Control c in ctrl)
{ if (c.Text.Trim() != "")
{
strSQL += c.Text.Trim() + ",";
} strSQL += ")"; }不管多少个都不增加代码量。
bool a=false;
执行的sql语句
string sql="select 表名 form ";
首先判断第一个
if(textBox1.Text.Trim() != "")
{
sql+="where ...." 条件自己写
a=true;
}
if(textBox2.Text.Trim() != ""){
if(a==true){
sql+="id=.."
}else{
sql+="where ..."
a=true;
}
{
if(textBox3.Text.Trim() != ""){
if(a==true){
sql+="id=.."
}else{
sql+="where...."
a=true;
}
}
if(textBox4.Text.Trim() != ""){
if(a==true){
sql+="id=.."
}else{
sql+="where ..." 如果是这个意思 请好好看看
a=true;
}
}
如果后面还有可以继续这样写的
//如果是AND条件
string sql="select * from table where ";
string where="1=1 ";if(textBox1.Text.Trim() != "")
{
where+=" AND "+条件1;
}if(textBox2.Text.Trim() != "")
{
where+=" AND "+条件2;
}...
sql+=where;//如果是OR条件
string sql="select * from table where ";
string where="1=2 ";if(textBox1.Text.Trim() != "")
{
where+=" OR "+条件1;
}if(textBox2.Text.Trim() != "")
{
where+=" OR "+条件2;
}...
sql+=where;//总之来说,只有一个一个地拼接了
界面上有4个输入框, 每个输入框的值作为查询条件(可以为空)。
4个框的值需要关联查询5个表
*/
create procedure GetInfo
(
@str1 varchar(50),--第一个输入框的值
@str2 varchar(50),--第2个输入框的值
@str3 varchar(50),
@str4 varchar(50)
)
select ..................我这样写估计你也不懂
建议你把表结构发出来
我帮你写
string s2=textbox2.text;
string s3=textbox3.text;string sql = "select * from table where (@s1=='' or columnName=@s1) and (@s2=='' or columnName=@s2) and (@s3=='' or columnName=@s3)"然后使用sqlcommand,把sql给commandtext,@s1,@s2,@s3作为sqlcommand的sqlParameter就可以了。
string s2=textbox2.text;
string s3=textbox3.text; string sql = "select * from table where (@s1 is null or columnName=@s1) and (@s2 is null or columnName=@s2) and (@s3 is null or columnName=@s3)" 然后使用sqlcommand,把sql给commandtext,@s1,@s2,@s3作为sqlcommand的sqlParameter就可以了。