比如我有一条语句:
sq="select * from people where name=%s and age=%d ";
在Delphi中我可以这样处理
sq:=Format(sq,[QuotedStr('张三',20),]);
//因为传给SQL之后张三这个字符串必须加上单撇号,所以我调用了Delphi中的一个函数QuotedStr来执行这个操作
最后传到SQL Server后的语句变成
select * from people where name='张三' and age=20在C#中怎么处理?
使得 sq="select * from people where name={0} and age={1} ";
格式化之后变成
最后传到SQL Server后的语句变成
select * from people where name='张三' and age=20

解决方案 »

  1.   

    可以用SQL参数,然后在程序里给参数赋值这个方法通用
      

  2.   

    实话说了吧 今天是第二天用C#(靠 要求写一些WebService服务) 有时会用DELPHI的一些方法来理解它
    我的SQL语句是拼凑的 最后直接传给SqlCommand执行 所以想使用C#里面的一些有关字符串格式化的功能 怎么用???
      

  3.   

    同意楼上的
    笨方法还有个
    sq="select * from people where name={0} and age={1} ";
    sq = sq.Replace("{0}","'张三'");
    sq = sq.Replace("{1}","20");
    之后也变了
    select * from people where name='张三' and age=20
      

  4.   

    string tmp="select * form {0} where {1}";
    string tmp1=String.Format(tmp,"table1","where1");
    this.Response.Write(tmp1);输出结果为
    select * form table1 where where1
      

  5.   

    楼上的没有明白我的意思 在C#中怎么处理?
    使得 sq="select * from people where name={0} and age={1} ";
    格式化之后变成
    最后传到SQL Server后的语句变成
    select * from people where name='张三' and age=20