protected void LinkButton2_Click(object sender, EventArgs e)
    {
        //SqlConnection con = new SqlConnection("server=(local);Database=sb_management;Uid=sa;Pwd=sa");
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
        string user = Session["sb_username"].ToString();
        string uid = "select finding from view_authority where uid=" + user;
        
        if (uid == "1")
        {
            Response.Write("<script language='javascript'>window.open(\"equipfind/mainfind_left.aspx\",\"main_left\");</script>");
        }
        else
        {
            Response.Write("<script>alert(\"对不起,您没有使用此功能的权限,请联系管理员!\");</script>");
            return;
        }
    }
这段代码中有个问题,就是查询语句并没有执行,导致UID是个SQL语句而无法进行判断,请高手帮我把缺少的代码写上或者把代码改正!谢谢,这个问题我不知道该怎么描述,上网查也查不到答案!

解决方案 »

  1.   

    无语
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); 
            string user = Session["sb_username"].ToString();
    SqlCommand command = new SqlCommand ("select finding from view_authority where uid=@uid", con);
    command.Parameters.Add(new SqlParameter("@uid", user));
    string ret = (string)command.ExecuteScalar();
    if (ret == "1")
    // 后面接你的代码
      

  2.   

    我晕你uid的值就是一个select finding from view_authority where uid=user名称
    怎么可能为为1
    你根本没有执行SQL语句啊
      

  3.   


    string query="SELECT FINDING FROM VIEW_AUTHORITY WHERE UID='"+USER+"'";
    SqlCommand mycom=new SqlCommand(query,con);
    mycom.CommandText = CommandType.Text;
    string uid=(string)mycom.ExecuteScalar();
    if(uid=="1")
    {}
    else
    {}
      

  4.   

    string uid = "select finding from view_authority where uid=" + user; 
    ----------------------
    string uid = "select finding from view_authority where uid='" + user+"'; 
    SqlCommand sqlcom=new SqlCommand(udi,con);
    string result=conver.Tosting(sqlcom.ExecuteScalar());
    result 就是查询到的结果。
    返回一个值就用SqlCommand的ExecuteScalar()方法;
      

  5.   

    感谢各位的教导,我把代码改成
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
            string user = Session["sb_username"].ToString();
            con.Open();
            string query = "select finding from view_authority where uid='" + user + "'";
            SqlCommand mycom = new SqlCommand(query, con);
            mycom.CommandType = CommandType.Text;
            string uid = (string)mycom.ExecuteScalar();
            if (uid == "1"){}
            con.Close();
    在调试的时候string uid = (string)mycom.ExecuteScalar();提示无法将类型为“System.Boolean”的对象强制转换为类型“System.String”!请问要怎么解决呢?
      

  6.   

    用楼上的办法result的值是true不是1,另外Parameters这个方法我不怎么会用,所以请高手用SQL语句的方法指点!
      

  7.   

    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); 
            string user = Session["sb_username"].ToString(); 
            con.Open(); 
            string query = "select finding from view_authority where uid='" + user + "'"; 
            SqlCommand mycom = new SqlCommand(query, con); 
            mycom.CommandType = CommandType.Text; 
            string uid = mycomcom.ExecuteNonQuery().ToString();
            if (uid == "1"){} 
            con.Close(); 
      

  8.   

    楼上的方法UID又变成-1了,我现在就只差个数据类型转换的问题了,我把1该成True就能实现我要的功能了,谢谢各位!
      

  9.   


    7楼的用法是错的,ExecuteNonQuery()方法是用来执行插入,删除,修改等SQL语句并返回影响的行数,如果是查询语句用它执行,那么不管你查询到多少条记录都是返回-1;所以不能用。
      

  10.   

    O(∩_∩)O哈哈~,9楼说的对,方法用错了,更改一下就可以了:
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); 
            string user = Session["sb_username"].ToString(); 
            con.Open(); 
            string query = "select finding from view_authority where uid='" + user + "'"; 
          SqlCommand mycom = new SqlCommand(query, con); 
            mycom.CommandType = CommandType.Text; 
            string uid = mycomcom.ExecuteScalar().ToString(); 
            if (uid == "1"){} 
            con.Close();