如何在存储过程中根据选择生成SELECT查询WHERE后的条件。如不用存储过程,在C#代码中可以通过以下的方法来实现自动生成SQL语查询语句。
int CategoryID=-1;
CategoryID = Convert.ToInt32(DropDownListCategory.SelectValue);
string ProductName=TextBoxProductName.Text;
string ProductModel=TextBoxDrawNumber.Text;
string DrawNumber=TexyBoxDrawNumber.Text;
int ProductType=-1;
ProductType=Convert.ToInt32(DropDownListProductType.SelectValue);
string CmdString="SELECT * FROM Product WHERE "
if(!string.IsNullOrEmpty(CategoryID))
{
CmdString += " CategoryID="+CategoryID.ToString();
}
if(!string.IsNullOrEmpty(ProductName))
{
CmdString += " AND ProductName="+ProductName;
}
if(!string.IsNullOrEmpty(DrawNumber))
{
CmdString += " AND DrawNumber="+DrawNumber;
}
if(!string.IsNullOrEmpty(ProductModel))
{
CmdString += " AND ProductModel="+ProductModel;
}
if(!string.IsNullOrEmpty(ProductType))
{
CmdString += " AND ProductType="+ProductType.ToString();
}
这样能根据用户选择的条件自动生成Where后的条件,请问在存储过程中应该怎么弄。