日期:2014-05-19  浏览次数:20994 次

c#中如何传入sql参数?
我想执行的是select   *   from   mytable   where   riqi   between   xx   and   yy.

其中xx和yy由用户输入,请问如何将参数传入?

我试的方法是:...   riqi   between   @riqi
这个方法,对于传入一个值是没问题的,但传入上述类似的范围会提示语法错误。

------解决方案--------------------
学习~
------解决方案--------------------
String _SQL= "select * from mytable where riqi between ' "+变量A+ " ' and ' "+变量B+ " ' ";
------解决方案--------------------
定义二个参数嘛
select * from mytable where riqi between @xx and @yy
------解决方案--------------------
String _SQL= "select * from mytable where riqi between "+ xx.ToString() + " and "+ yy.ToString()+ " ";
------解决方案--------------------
string strSql= "select * from mytable where riqi between @XX and @YY "

SqlCommand cmd=new SqlCommand(strSql,conn);
cmd.Parameters.Add( "@XX ",SqlDBType.DateTime);
cmd.Parameters.Add( "@YY ",SqlDBType.DateTime);
cmd.Parameters[ "@XX "].value=你要给的;
cmd.Parameters[ "@YY "].value=你要给的;

....