SelectCommand参数问题
使用SelectCommand进行查询,为sql设置有两个检索条件c和d。
想实现的需求是
当设置查询条件c为1而不给d赋值时,检索出符合c=1的数据(无论d是什么都无所谓,都看作是符合条件的检索结果)
当设置查询条件d为2而不给c赋值时,检索出符合d=2的数据(无论c是什么都无所谓,都看作是符合条件的检索结果)
当设置查询条件c为1而d为2时,检索出符合c=1并且d=2的数据
不想用直接拼sql的方式实现,而想用SelectCommand并且设置参数的方式实现,下面的代码肯定不行,但不知道如何实现,请高人指点,呵呵
SelectCommand`.commandtext = select a,b from table where c= @CategoryName and d = @SerialNum
SelectCommand.Parameters.Add(
"@CategoryName", SqlDbType.VarChar, 80).Value = "1";
SelectCommand.Parameters.Add(
"@SerialNum", SqlDbType.Int).Value = 2;
categoriesAdapter.Fill(categoriesDataSet);
[解决办法]
- VB.NET code
if vc<>"" then if vd<>"" then sql="select a,b from table where c='" & vc &"' and d ='" & vd &"'" else sql="select a,b from table where c='" & vc &"'" end if else if vd<>"" then sql="select a,b from table where d ='" & vd &"'" else sql="select a,b from table" end ifend if