把条件分行、分级写 if( (a | b) && ( (b & c) && (c == 2) ) || (a == c) ) {} else {}
switch(条件) { case 条件1: {}break; case 条件2: {}break; case 条件3: {}break; case 条件4: {}break; case 条件1: {}break; default:break; } 如果想有更好的效率可以参考设计模式的状态管理模式,State Partten 就是为了消灭太多的选择语句而产生的。。
if( (a | b) &&
( (b & c) &&
(c == 2)
) ||
(a == c)
)
{}
else
{}
{
case 条件1:
{}break;
case 条件2:
{}break;
case 条件3:
{}break;
case 条件4:
{}break;
case 条件1:
{}break;
default:break;
}
如果想有更好的效率可以参考设计模式的状态管理模式,State Partten 就是为了消灭太多的选择语句而产生的。。
{
case case1:
break;
case case1:
break;
default:
break;
}if(...)
{
}
else if(..)
{
}
else if(...)
{
}
.
.
.
else
{
}
{
case 0:
break;
case 1:
break;
default:
break;
}如果是复合条件那就只能用:
if(f == 0 && f < x)
{
;
}
else if(f > x)
{
;
}
else if(f == x || x < 60)
{
;
}
else
{
;
}
{
case 条件1:
{}break;
case 条件2:
{}break;
case 条件3:
{}break;
case 条件4:
{}break;
case 条件1:
{}break;
default:break;
}
可以用switch来做
如果条件不一样的话,那你只能用
if(...)
{
}
else if(..)
{
}
else if(...)
{
SqlConnection Conn1 = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=gem");
String deleteCmd1 = "DELETE FROM 临时信息";
SqlCommand myCommand1 = new SqlCommand(deleteCmd1, Conn1);
myCommand1.Connection.Open();
myCommand1.ExecuteNonQuery();
myCommand1.Connection.Close(); SqlCommand command1 = new SqlCommand ("insert into 临时信息(商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注) select 商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注 from 商品信息 where 采购价格>='"+ Double.Parse(TextBox2.Text) +"' and 采购价格<='"+ Double.Parse( TextBox3.Text) + "'and 时间>='"+DateTime.Parse(TextBox4.Text)+"' and 时间<='"+DateTime.Parse(TextBox5.Text)+"'ORDER BY 时间 DESC", Conn1);
command1.Connection.Open();
command1.ExecuteNonQuery();
command1.Connection.Close();
Conn1.Close();
Response.Write("<script>alert('111111111')</script>");
}
else if(DropDownList1.SelectedItem.Value.ToString().Trim()=="全部"|| DropDownList2.SelectedItem.Value.ToString().Trim()=="全部"|| TextBox1.Text.Trim()!="全部")
{
SqlConnection Conn2 = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=gem"); String deleteCmd2 = "DELETE FROM 临时信息";
SqlCommand myCommand2 = new SqlCommand(deleteCmd2, Conn2);
myCommand2.Connection.Open();
myCommand2.ExecuteNonQuery();
myCommand2.Connection.Close(); SqlCommand command2 = new SqlCommand ("insert into 临时信息(商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注) select 商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注 from 商品信息 where 类别2='"+DropDownList2.SelectedItem.ToString().Trim()+"' and 采购价格>='"+Double.Parse(TextBox2.Text)+"' and 采购价格<='"+Double.Parse(TextBox3.Text)+"'and 时间>='"+DateTime.Parse(TextBox4.Text)+"' and 时间<='"+DateTime.Parse(TextBox5.Text)+"'ORDER BY 时间 DESC", Conn2);
command2.Connection.Open();
command2.ExecuteNonQuery();
command2.Connection.Close();
Conn2.Close(); Response.Write("<script>alert('22222222')</script>");
}
else if(DropDownList1.SelectedItem.Value.ToString().Trim()=="全部"|| DropDownList2.SelectedItem.Value.ToString().Trim()!="全部"|| TextBox1.Text.Trim()!="全部")
{
SqlConnection Conn3 = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=gem");
String deleteCmd3 = "DELETE FROM 临时信息";
SqlCommand myCommand3 = new SqlCommand(deleteCmd3, Conn3);
myCommand3.Connection.Open();
myCommand3.ExecuteNonQuery();
myCommand3.Connection.Close(); SqlCommand command3 = new SqlCommand ("insert into 临时信息(商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注) select 商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注 from 商品信息 where 供应商='"+ TextBox1.Text.Trim() +"' and 采购价格>='"+Double.Parse(TextBox2.Text)+"' and 采购价格<='"+Double.Parse(TextBox3.Text)+"'and 时间>='"+DateTime.Parse(TextBox4.Text)+"' and 时间<='"+DateTime.Parse(TextBox5.Text)+"'ORDER BY 时间 DESC", Conn3);
command3.Connection.Open();
command3.ExecuteNonQuery();
command3.Connection.Close();
Conn3.Close();
Response.Write("<script>alert('33333333')</script>");
}
else if(DropDownList1.SelectedItem.Value.ToString().Trim()=="全部"|| DropDownList2.SelectedItem.Value.ToString().Trim()!="全部"|| TextBox1.Text.Trim()!="全部")
{
SqlConnection Conn4 = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=gem");
String deleteCmd4 = "DELETE FROM 临时信息";
SqlCommand myCommand4 = new SqlCommand(deleteCmd4, Conn4);
myCommand4.Connection.Open();
myCommand4.ExecuteNonQuery();
myCommand4.Connection.Close();
SqlCommand command4 = new SqlCommand ("insert into 临时信息(商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注) select 商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注 from 商品信息 where 类别2='"+ DropDownList2.SelectedItem.ToString().Trim()+"'and 供应商='"+TextBox1.Text.Trim()+"'and 采购价格>='"+Double.Parse(TextBox2.Text.Trim())+"'and 采购价格<='"+Double.Parse(TextBox3.Text.Trim())+"'and 时间>='"+DateTime.Parse(TextBox4.Text.Trim())+"' and 时间<='"+DateTime.Parse(TextBox5.Text.Trim())+"'ORDER BY 时间 DESC", Conn4);
command4.Connection.Open();
command4.ExecuteNonQuery();
command4.Connection.Close();
Conn4.Close();
Response.Write("<script>alert('4444444444')</script>");
}
{
SqlConnection Conn5 = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=gem");
String deleteCmd5 = "DELETE FROM 临时信息";
SqlCommand myCommand5 = new SqlCommand(deleteCmd5, Conn5);
myCommand5.Connection.Open();
myCommand5.ExecuteNonQuery();
myCommand5.Connection.Close();
SqlCommand command5 = new SqlCommand ("insert into 临时信息(商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注) select 商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注 from 商品信息 where 类别1='"+DropDownList1.SelectedItem.ToString().Trim()+"' and 类别2='"+DropDownList2.SelectedItem.ToString().Trim()+"' and 供应商='"+TextBox1.Text.Trim()+"' and 采购价格>='"+Double.Parse(TextBox2.Text.Trim())+"' and 采购价格<='"+Double.Parse(TextBox3.Text.Trim())+"'and 时间>='"+DateTime.Parse(TextBox4.Text.Trim())+"' and 时间<='"+DateTime.Parse(TextBox5.Text.Trim())+"'ORDER BY 时间 DESC", Conn5);
command5.Connection.Open();
command5.ExecuteNonQuery();
command5.Connection.Close();
Conn5.Close();
Response.Write("<script>alert('5555555555')</script>");
}
else if(DropDownList1.SelectedItem.Value.ToString().Trim()!="全部"|| DropDownList2.SelectedItem.Value.ToString().Trim()!="全部"|| TextBox1.Text.Trim()!="全部")
{
SqlConnection Conn6 = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=gem");
String deleteCmd6 = "DELETE FROM 临时信息";
SqlCommand myCommand6 = new SqlCommand(deleteCmd6, Conn6);
myCommand6.Connection.Open();
myCommand6.ExecuteNonQuery();
myCommand6.Connection.Close();
SqlCommand command6 = new SqlCommand ("insert into 临时信息(商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注) select 商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注 from 商品信息 where 类别1='"+DropDownList1.SelectedItem.ToString().Trim()+"' and 类别2='"+DropDownList2.SelectedItem.ToString().Trim()+"' and 采购价格>='"+Double.Parse(TextBox2.Text.Trim())+"' and 采购价格<='"+Double.Parse(TextBox3.Text.Trim())+"'and 时间>='"+DateTime.Parse(TextBox4.Text.Trim())+"' and 时间<='"+DateTime.Parse(TextBox5.Text.Trim())+"'ORDER BY 时间 DESC", Conn6);
command6.Connection.Open();
command6.ExecuteNonQuery();
command6.Connection.Close();
Conn6.Close();
Response.Write("<script>alert('666666666')</script>");
}
else if(DropDownList1.SelectedItem.Value.ToString().Trim()!="全部"|| DropDownList2.SelectedItem.Value.ToString().Trim()=="全部"|| TextBox1.Text.Trim()!="全部")
{
SqlConnection Conn7 = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=gem");
String deleteCmd7 = "DELETE FROM 临时信息";
SqlCommand myCommand7 = new SqlCommand(deleteCmd7, Conn7);
myCommand7.Connection.Open();
myCommand7.ExecuteNonQuery();
myCommand7.Connection.Close();
SqlCommand command7 = new SqlCommand ("insert into 临时信息(商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注) select 商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注 from 商品信息 where 类别1='"+DropDownList1.SelectedItem.ToString().Trim()+"' and 供应商='"+TextBox1.Text.Trim()+"' and 采购价格>='"+Double.Parse(TextBox2.Text.Trim())+"' and 采购价格<='"+Double.Parse(TextBox3.Text.Trim())+"'and 时间>='"+DateTime.Parse(TextBox4.Text.Trim())+"' and 时间<='"+DateTime.Parse(TextBox5.Text.Trim())+"'ORDER BY 时间 DESC", Conn7);
command7.Connection.Open();
command7.ExecuteNonQuery();
command7.Connection.Close();
Conn7.Close();
Response.Write("<script>alert('777777777')</script>");
}
else
// DropDownList1.SelectedItem.Value.ToString().Trim()!="全部"|| DropDownList2.SelectedItem.Value.ToString().Trim()=="全部"|| TextBox1.Text.Trim()!="全部")
{
SqlConnection Conn8 = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=gem");
String deleteCmd8 = "DELETE FROM 临时信息";
SqlCommand myCommand8 = new SqlCommand(deleteCmd8, Conn8);
myCommand8.Connection.Open();
myCommand8.ExecuteNonQuery();
myCommand8.Connection.Close();
SqlCommand command8 = new SqlCommand ("insert into 临时信息(商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注) select 商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注 from 商品信息 where 类别1='"+DropDownList1.SelectedItem.ToString().Trim()+"' and 类别2='"+DropDownList2.SelectedItem.ToString().Trim()+"'and 供应商='"+TextBox1.Text.Trim()+"' and 采购价格>='"+Double.Parse(TextBox2.Text.Trim())+"' and 采购价格<='"+Double.Parse(TextBox3.Text.Trim())+"'and 时间>='"+DateTime.Parse(TextBox4.Text.Trim())+"' and 时间<='"+DateTime.Parse(TextBox5.Text.Trim())+"'ORDER BY 时间 DESC", Conn8);
command8.Connection.Open();
command8.ExecuteNonQuery();
command8.Connection.Close();
Conn8.Close();
Response.Write("<script>alert('888888888')</script>");
}看看代码,还是不行,老是乱!!
{
DoSomeWorkUnderCondition1();
}
else if( Condition2 )
{
DoSomeWorkUnderCondition2();
}
else if( Condition3 )
{
DoSomeWorkUnderCondition3();
}
.....
{
// try
// {
if(DropDownList1.SelectedItem.Value.ToString().Trim()=="全部"|| DropDownList2.SelectedItem.Value.ToString().Trim()=="全部"|| TextBox1.Text.Trim()=="全部")
{
SqlConnection Conn1 = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=gem");
String deleteCmd1 = "DELETE FROM 临时信息";
SqlCommand myCommand1 = new SqlCommand(deleteCmd1, Conn1);
myCommand1.Connection.Open();
myCommand1.ExecuteNonQuery();
myCommand1.Connection.Close(); SqlCommand command1 = new SqlCommand ("insert into 临时信息(商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注) select 商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注 from 商品信息 where 采购价格>='"+ Double.Parse(TextBox2.Text) +"' and 采购价格<='"+ Double.Parse( TextBox3.Text) + "'and 时间>='"+DateTime.Parse(TextBox4.Text)+"' and 时间<='"+DateTime.Parse(TextBox5.Text)+"'ORDER BY 时间 DESC", Conn1);
command1.Connection.Open();
command1.ExecuteNonQuery();
command1.Connection.Close();
Conn1.Close();
Response.Write("<script>alert('111111111')</script>"); }
else if(DropDownList1.SelectedItem.Value.ToString().Trim()=="全部"|| DropDownList2.SelectedItem.Value.ToString().Trim()=="全部"|| TextBox1.Text.Trim()!="全部")
{
SqlConnection Conn2 = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=gem"); String deleteCmd2 = "DELETE FROM 临时信息";
SqlCommand myCommand2 = new SqlCommand(deleteCmd2, Conn2);
myCommand2.Connection.Open();
myCommand2.ExecuteNonQuery();
myCommand2.Connection.Close(); SqlCommand command2 = new SqlCommand ("insert into 临时信息(商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注) select 商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注 from 商品信息 where 类别2='"+DropDownList2.SelectedItem.ToString().Trim()+"' and 采购价格>='"+Double.Parse(TextBox2.Text)+"' and 采购价格<='"+Double.Parse(TextBox3.Text)+"'and 时间>='"+DateTime.Parse(TextBox4.Text)+"' and 时间<='"+DateTime.Parse(TextBox5.Text)+"'ORDER BY 时间 DESC", Conn2);
command2.Connection.Open();
command2.ExecuteNonQuery();
command2.Connection.Close();
Conn2.Close(); Response.Write("<script>alert('22222222')</script>");
}.................else
// DropDownList1.SelectedItem.Value.ToString().Trim()!="全部"|| DropDownList2.SelectedItem.Value.ToString().Trim()=="全部"|| TextBox1.Text.Trim()!="全部")
{
SqlConnection Conn8 = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=gem");
String deleteCmd8 = "DELETE FROM 临时信息";
SqlCommand myCommand8 = new SqlCommand(deleteCmd8, Conn8);
myCommand8.Connection.Open();
myCommand8.ExecuteNonQuery();
myCommand8.Connection.Close();
SqlCommand command8 = new SqlCommand ("insert into 临时信息(商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注) select 商品编号,供应编号,名称,类别1,类别2,重量,采购价格,销售价格,供应商,图片名称,图片类型,图片,时间,说明,备注 from 商品信息 where 类别1='"+DropDownList1.SelectedItem.ToString().Trim()+"' and 类别2='"+DropDownList2.SelectedItem.ToString().Trim()+"'and 供应商='"+TextBox1.Text.Trim()+"' and 采购价格>='"+Double.Parse(TextBox2.Text.Trim())+"' and 采购价格<='"+Double.Parse(TextBox3.Text.Trim())+"'and 时间>='"+DateTime.Parse(TextBox4.Text.Trim())+"' and 时间<='"+DateTime.Parse(TextBox5.Text.Trim())+"'ORDER BY 时间 DESC", Conn8);
command8.Connection.Open();
command8.ExecuteNonQuery();
command8.Connection.Close();
Conn8.Close();
Response.Write("<script>alert('888888888')</script>");
}
// Response.Redirect("repshangpin.aspx");// }
// catch
// {
// Response.Write("<script>alert('服务器出错,请与管理员联系!')</script>");
// }