书上说Command提供程序不支持命令参数,必须使用@占位符,例如:
Command1.CommandText = "select sno, sname, sage FROM student WHERE sdept =@sdept"可是书上的例子:
if(combox1.Text == "计算机系")
command2.CommandText = "select sno, sname, sage FROM student WHERE (sdept = '计算机系')";
这个例子又没有使用占位符.....我有点糊涂了
如果可以不用占位符是不是又可以写成
string sql ="select sno, sname, sage FROM student WHERE sdept = '+sdept+';
是存储过程必须用@,只要用@这个的就是存储过程是么?
老师说用存储过程来做这个DataGridView,那么怎样的才叫存储过程?

解决方案 »

  1.   

    不知道  
    我也是新手
    string sql ="select sno, sname, sage FROM student WHERE sdept = '+sdept+';
    我都是么写的
    存储过程在数据库里写
    没写的
    @变量
      

  2.   

    用@作为占位符的  一般使用参数 
    不用参数时,可以这样写
    string sql ="select sno, sname, sage FROM student WHERE sdept = '"+sdept+"'";//sdept是文本类型,没有用到参数  可以直接写 不用@占位符你现在完全可以把存储过程理解成带参数的sql语句(但这是不对的),目前这样理解,以后就会知道Command1.CommandText = "select sno, sname, sage FROM student WHERE sdept =@sdept"
    Command1.Parameters.Add(new SqlParameter("@sdept",参数值)); // 给参数@sdept赋值
      

  3.   

    一般查询语句的sql为
    string sql="select * from Tb where username='"+username+"'"
    SqlParameter表示SqlCommand的参数
    SqlParameter类型的数组作为SqlCommand的参数存在,配合转义字符@
      

  4.   

    一种方法是:string sql ="select sno, sname, sage FROM student WHERE sdept = '"+sdept+"'";
      

  5.   

    书上说Command提供程序不支持命令参数,必须使用@占位符,例如:
    Command1.CommandText = "select sno, sname, sage FROM student WHERE sdept =@sdept"SqlParameter表示SqlCommand的参数
    SqlParameter类型的数组作为SqlCommand的参数存在,配合转义字符@也就是用到SqlParameter时,必须使用@占位符,是这个意思吧
      

  6.   


    用@作为占位符的  一般使用参数 ..........这个参数指:@sdept,还是指:SqlParameter表示SqlCommand的参数
    存储过程理解成带参数的sql语句...有@符号的sql语句就是存储过程...这样理解么?
      

  7.   

    Command1.CommandText = "select sno, sname, sage FROM student WHERE sdept =@sdept"
    这样不行?if(combox1.Text == "计算机系")
    command2.CommandText = "select sno, sname, sage FROM student WHERE (sdept = '计算机这样可以。
    你自己看看吧?
      

  8.   

    SQL语句中没有参数的进行的是无参查询,有占位符或用变量设置参数的查询是参数化查询,使用占位符的查询可以用命令对象的Parameters参数属性的方法添加相应参数并进行值的设置
      

  9.   

    用占位符和用字符串拼接都可以,但用占位符比较安全,可以防止黑客sql注入,有关sql注入的东西,你可以在网站上在查查。实在不会,我们可以一起探讨^_^
      

  10.   

    至少这个东西可以轻易发现一个bug,当sdept中包含单引号的时候怎么办?
      

  11.   

    关于在sql语句中使用参数,并且查询时提供参数的值,实际上只要多看几个例子就可以了,或者google,例如:

    http://www.google.com.hk/search?q=DbParameter+SqlParameter&hl=zh-CN&newwindow=1&safe=strict&rls=com.microsoft:zh-cn:IE-SearchBox&prmd=df&source=lnt&tbs=lr:lang_1zh-CN%7Clang_1zh-TW&lr=lang_zh-CN%7Clang_zh-TW&sa=X&ei=aeRKTKjqKIe6cY73seUM&ved=0CAcQpwU
    自己解决这个问题,别仅仅看一本书(容易把眼睛看坏)。
      

  12.   

    Command提供程序不支持命令参数,必须使用@占位符,例如:
    [color=#0000FF]Command1.CommandText = "select sno, sname, sage FROM student WHERE sdept =@sdept"

    这个命令参数是什么?在C#中存储过程叫什么?什么意思?怎么才能知道它是存储过程?[/color]
      

  13.   

    1.占位符 根据不同的数据库 不懂 例如 MS-SQL 是@ 而 ORACLE 是:
    2.存储过程是在数据库里面编译好的一段语句
    例如
         /// <summary>
        /// 获取某商品信息
        /// </summary>
        /// <param name="theOrgCode">店号</param>
        /// <param name="thePluCode">商品编号</param>         public static DataSet GetPluProject(string theOrgCode, string thePluCode)
        {
            SqlParameter[] sortPara = { new SqlParameter("@PluCode", thePluCode),
                                        new SqlParameter("@OrgCode", theOrgCode)
                                          };
            return SqlHelper.ExecuteDataset(SqlHelper.SQLCONN_CosSysDb, CommandType.StoredProcedure, " GetPluProject", sortPara);
        }
    REATE PROCEDURE [dbo].[yjGetPluProject]
    (
    @OrgCode char(10),
    @PluCode char(10)            
    )
    AS
    Select Goods.PluCode,
           PluName,
           IsNull(BarCode, '') as BarCode,
       GCount,
           CCount,
           AskCnt,
       InitCount,
       Spec,
           IsNull(CargoNo, '') as CargoNo,
           Unit,
           Goods.JPrice,
       Goods.NetJPrice,
       JTaxRate,
       Goods.Price,
       DepCode,
       Goods.Clscode,
       VendorCode,
       IsNull(Goods.PsPrice,Goods.JPrice) as PsPrice
    From Goods, GClass C, GoodsOrg
    where Goods.PluCode = GoodsOrg.PluCode
          and GoodsOrg.OrgCode = @OrgCode
          and Goods.ClsCode = C.ClsCode
          and PluType = '0'
          and (PluStatus = '0' or PluStatus = '' or PluStatus = '1')
          and exists (select PluCode
                      from GoodsOrg
                      where PluCode = Goods.PluCode
                       and OrgCode = @OrgCode
                       and IsAsk = '1')
          and Goods.Plucode = @PluCode
    GO
    REATE PROCEDURE [dbo].[yjGetPluProject]
    (
    @OrgCode char(10),
    @PluCode char(10)            
    )
    AS
    Select Goods.PluCode,
           PluName,
           IsNull(BarCode, '') as BarCode,
       GCount,
           CCount,
           AskCnt,
       InitCount,
       Spec,
           IsNull(CargoNo, '') as CargoNo,
           Unit,
           Goods.JPrice,
       Goods.NetJPrice,
       JTaxRate,
       Goods.Price,
       DepCode,
       Goods.Clscode,
       VendorCode,
       IsNull(Goods.PsPrice,Goods.JPrice) as PsPrice
    From Goods, GClass C, GoodsOrg
    where Goods.PluCode = GoodsOrg.PluCode
          and GoodsOrg.OrgCode = @OrgCode
          and Goods.ClsCode = C.ClsCode
          and PluType = '0'
          and (PluStatus = '0' or PluStatus = '' or PluStatus = '1')
          and exists (select PluCode
                      from GoodsOrg
                      where PluCode = Goods.PluCode
                       and OrgCode = @OrgCode
                       and IsAsk = '1')
          and Goods.Plucode = @PluCode
    GO--存储过程如下
    REATE PROCEDURE [dbo].[GetPluProject]
    (
    @OrgCode char(10),
    @PluCode char(10)            
    )
    AS
    Select Goods.PluCode,
           PluName,
           IsNull(BarCode, '') as BarCode,
       GCount,
           CCount,
           AskCnt,
       InitCount,
       Spec,
           IsNull(CargoNo, '') as CargoNo,
           Unit,
           Goods.JPrice,
       Goods.NetJPrice,
       JTaxRate,
       Goods.Price,
       DepCode,
       Goods.Clscode,
       VendorCode,
       IsNull(Goods.PsPrice,Goods.JPrice) as PsPrice
    From Goods, GClass C, GoodsOrg
    where Goods.PluCode = GoodsOrg.PluCode
          and GoodsOrg.OrgCode = @OrgCode
          and Goods.ClsCode = C.ClsCode
          and PluType = '0'
         and exists (select PluCode
                      from GoodsOrg
                      where PluCode = Goods.PluCode
                       and OrgCode = @OrgCode
                       and IsAsk = '1')
          and Goods.Plucode = @PluCode
    GO