我现在有一个sql语句;int b=1;
string sql = "select *  from a where b = @b ";
这个在.aspx能正常执行.但到.ashx就提示=附近有错.要改成这样才能执行string sql = "select *  from a where b = " +@b;//不知为什么,这样写安全吗

解决方案 »

  1.   

    区别就是后者要声明@b为一个string,从这个意义上将前者是安全的,后者不安全
      

  2.   


    支持楼上,前者安全"=附近有错"应该是ADO.NET对象报出的错误
    楼主监视一下sql对象最终传入ADO.NET对象中的值
      

  3.   

    是我搞错了, string sql = "select *  from  @b where class = @a ";
            SqlCommand com = new SqlCommand(sql, H_data.con);
            SqlParameter[] prams ={new SqlParameter("@a", SqlDbType.Int ),
                                 new SqlParameter("@b", SqlDbType.VarChar ,50)
                                  };
            prams[0].Value = a; prams[1].Value = b;
    SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection);//提示要声明变量@b
      

  4.   

    string sql = "select *  from  "+@b +"where class = @a ";//分开才行
      

  5.   

    我不是高手,但是我做过这样的程序,好像这样写可以,不知道为什么??string sql = "select *  from  @a where class = @b ";
            SqlCommand com = new SqlCommand(sql, H_data.con);
            SqlParameter[] prams ={new SqlParameter("@a", SqlDbType.VarChar ,50),
                                 new SqlParameter("@b", 50SqlDbType.Int )
                                  };
            prams[0].Value = a; prams[1].Value = b;
    SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection);//提示要声明变量@b顺便请教一些这个顺序会有影响吗?我做过的就是这样,顺序错了,就是出错!!