首先改写 private void runsql(int a,string b) { if(a != null) this.SC_select.Parameters[0].Value=a; else this.SC_select.Parameters[0].Value=DBNull.Value; ...... }其次要改写你的查询 可以加上判断,也可以写成 select * from t where field1 like '@par1' 要取全部的话this.SC_select.Parameters[0].Value="%";
函数可以改为: private void runsql(int a,string b) { if( a != null ) this.SC_select.Parameters[0].Value=a; else this.SC_select.Parameters[0].Value=DBNull.Value; if( b != null ) this.SC_select.Parameters[1].Value=b; else this.SC_select.Parameters[1].Value=DBNull.Value; }
private void runsql(int a,string b)
{
if(a != null)
this.SC_select.Parameters[0].Value=a;
else
this.SC_select.Parameters[0].Value=DBNull.Value;
......
}其次要改写你的查询
可以加上判断,也可以写成 select * from t where field1 like '@par1'
要取全部的话this.SC_select.Parameters[0].Value="%";
private void runsql(int a,string b)
{
if( a != null )
this.SC_select.Parameters[0].Value=a;
else
this.SC_select.Parameters[0].Value=DBNull.Value; if( b != null )
this.SC_select.Parameters[1].Value=b;
else
this.SC_select.Parameters[1].Value=DBNull.Value;
}
另外,楼上说的也在理,存储过程也不够健壮。
//this.runsql(DBNull.Value,DBNull.Value) //这样也报错--->类型都不匹配,当然会报错!this.SC_select.Parameters[0].Value=DBNull.Value; //注意这里是不能给Null的
this.SC_select.Parameters[1].Value=DBNull.Value; //同上这样给的应该是可以,数据不对,是你的存储过程的问题,
在存储过程中判断一下,if fields is Null
就不用Where条件直接查出所有记录!
函数的参数中的int类型是不能设成null来调用的!to Eddie005(暴走005)
你的做法不大可靠.传值,就一定要传一个明确的值过去,不然可能会有不可预料的后果;to jkflyfox(飞狐)
估计你处理我这类问题的话用的是构建动态SQL句子来查询的