日期:2014-05-18  浏览次数:21020 次

新手问题C#中的变量如何传给SQL
新手问题C#中的变量如何传给SQL,比如SqlCommand myCommand = new SqlCommand("select zkzh,school,name,n1,fzf from yw01 where substring(zkzh,5,3)='001'"我想将'001'换成一个变量fkd,应该用什么方法,是不是在变量前加什么字符?

------解决方案--------------------
如:String str="select zkzh,school,name,n1,fzf from yw01 where aaa='"+aaa+"'";//字符串型变量
String str="select zkzh,school,name,n1,fzf from yw01 where aaa="+aaa;//int型变量
------解决方案--------------------
T-SQL参数加@前导符...

例...
C# code

string fzf = "001";
SqlCommand myCommand = new SqlCommand("select zkzh,school,name,n1,fzf from yw01 where substring(zkzh,5,3)=@fzf";
cmd.Parameters.Add("@fzf",fzf);