打印

[.net] 请大虾帮忙下vs2005和sql sever2005连接问题

我的代码执行到cmd.ExecuteReader()就不行了,直接跳到catch中去了,请大虾指点。
情况紧急!!
谢谢!


public partial class _Default : System.Web.UI.Page
{
    public static bool blCanLogin = false;  //记录能否检验是否通过
    string userid;
    string userpwd;
    public static string strUserId = "";      //记录用户号
    public string strconn = "Data Source=PLA771DF721C3F8\\SQLEXPRESS;Initial Catalog=bs;Integrated Security=True";
    public string sendStrSQL;
    public string userrole;
   
    //static int count=0;
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
   
   
    protected void Button2_Click(object sender, EventArgs e)
    {
        pwd.Text = "";

    }
   
    protected void ok_button_Click(object sender, EventArgs e)
    {
        
        SqlConnection conn=new SqlConnection(strconn);
        
        userid = user_id.Text.ToString().Trim();
        userpwd = pwd.Text.ToString().Trim();

        sendStrSQL = "SELECT * from login.info where UserId='+userid+'";

        userrole = rbl.SelectedValue.Trim();
        
        try
        {
            conn.Open();

            SqlCommand cmd = new SqlCommand(sendStrSQL,conn);
            
            
            SqlDataReader dr = cmd.ExecuteReader();//到这边就执行不了            
            
         if(dr.Read())
            {

                if (userpwd == dr["UserPwd"].ToString())
                {
                    Response.Redirect("UserInfoMage.aspx");
                    Session["userid"] = userid;
                    Session["userrole"] = userrole;
                    dr.Close();
                    conn.Close();
                    return;
                 }
                 else Response.Write("<Script>alert('密码错误!');history.back(-1);</Script>");
            }
          else Response.Write("<Script>alert('用户名错误!');history.back(-1);</Script>");

            
            
            /* if (dr.Read())
            {
                if (dr.GetString(2) == userpwd)
                {
                    Session["userid"] = userid;
                    Session["userrole"] = userrole;
                    conn.Close();
                }
                else Response.Write("<Script>alert('密码错误!');history.back(-1);</Script>");
            }
            else Response.Write("<Script>alert('用户名错误!');history.back(-1);</Script>");*/
       }
       catch (Exception ee)
        {
            Response.Write("<Script language=javascript>alert('"+ee.Message.ToString()+"')</Script>");
            
       }
        finally
        {
           conn.Close();
        }
   
        
        

    }
sendStrSQL = "SELECT * from login.info where UserId='+userid+'";

检查一下这句
修行的魔法师
我现在把
sendStrSQL = "SELECT * from login.info where UserId='+userid+'";
改为:sendStrSQL = "SELECT * from [login.info] where UserId='+userid+'";
这样ExecuteReader()可以执行了,但是
userpwd == dr["UserPwd"].ToString()中等号后面却没有数据,userpwd是我从前台文本框中得到的字符串,我想问是否是我的
dr["UserPwd"].ToString()格式有问题


另非常感谢2L
我现在把
sendStrSQL = "SELECT * from login.info where UserId='+userid+'";
改为:sendStrSQL = "SELECT * from [login.info] where UserId='"+userid+"'";
这样ExecuteReader()可以执行了,但是
userpwd == dr["UserPwd"].ToString()中等号后面却没有数据,userpwd是我从前台文本框中得到的字符串,我想问是否是我的
dr["UserPwd"].ToString()格式有问题


另非常感谢2L

[ 本帖最后由 momenteve 于 2008-5-11 14:25 编辑 ]

TOP

还在为头像烦恼?还在为不能关注好友动态烦忧?快来蓝色理想家园吧!
前台文本框应该是 文本框名.Text

TOP

既然知道catch,为什么不直接分析catch出来的异常信息

TOP

"SELECT * from [login.info] where UserId='"+userid+"'";
这样ExecuteReader()可以执行

楼主 [login.info] 是一个表或一个过程或一个视图
login.info 是 login 表下的 info字段
http://www.qlili.com 个人站帮点啊

TOP