从sql server库里查询出选定条件的记录,条件是session变量
Session["UserName"]=ST_read[0].ToString();
..........
sql="select * from user where name='Session["UserName"]'"
这句应该怎么写呢?请前辈们指教一下。

解决方案 »

  1.   

    sql = string.Format("select * from user where name='{0}'",Session["UserName"]);
      

  2.   


    sql="select * from user where name='"+Session["UserName"]+"'" 这样拼接就可以了
      

  3.   

    上面是C#生成SQL的语句,生成的SQL应该类似于:select * from user where name='某一个用户名'
      

  4.   

    先把Session["UserName"]赋值到一个字符串,然后再拼串
      

  5.   

    sql="select * from user where name='"+Session["UserName"].ToString+"'" 
      

  6.   

    sql = "select * from user where name = '"+Session["UserName"].Tostring()+"'";
      

  7.   


    sql="select * from user where name='"+Session["UserName"]+"'" 
      

  8.   

    不好意思  Session["UserName"] 忘了加 .tostring();
      

  9.   

    sql="select * from [user] where name = @name";
    SqlConnection cn = new SqlConnection(@"server=.\sqlexpress;uid=sa;pwd=;database=yourdatabase");
    SqlCommand cmd = new SqlCommand(sql, cn);
    //前面一定要判断Session["UserName"]不为null
    cmd.Parameters.AddWithValue("@name", Session["UserName"].ToString()); 
      

  10.   

    sql="select * from user where name='"+Session["UserName"].ToString+"'"