Button1_Click方法中没有连接数据库怎么取!

解决方案 »

  1.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    }//按“确定”后检验用户名和密码是否正确?
    private void Button1_Click(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    //建立连接
    string MyConnString = "user id=sa;password=;initial catalog=jrsl;data source=(local);connect timeout=30";
    string strSel = "Select * from userinfo";
    SqlConnection MyConn = new SqlConnection(MyConnString);
    SqlCommand MyComm = new SqlCommand(strSel,MyConn);
    MyComm.Connection.Open(); //读取数据
    string NameString;
    string PString;
    SqlCommand cmd=new SqlCommand("SELECT PASSWORD FORM USERINFO where userid='"+txtUserID.Text+"'");
    string pwd =Convert.ToString(cmd.ExecuteScalar()); //检验用户名和密码是否相同
                               ...
    }
      

  2.   

    sorry://按“确定”后检验用户名和密码是否正确?
    private void Button1_Click(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    //建立连接
    string MyConnString = "user id=sa;password=;initial catalog=jrsl;data source=(local);connect timeout=30";
    string strSel = "Select * from userinfo";
    SqlConnection MyConn = new SqlConnection(MyConnString);
    SqlCommand MyComm = new SqlCommand(strSel,MyConn);
    MyComm.Connection.Open(); //读取数据
    string NameString;
    string PString;
    MyComm.CommandText = "SELECT PASSWORD FORM USERINFO where userid='"+txtUserID.Text+"'";
    string pwd =Convert.ToString(MyComm.ExecuteScalar()); //检验用户名和密码是否相同
                               ...
    }
      

  3.   

    //在Page_Load是建立数据库连接
    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here

    }//按“确定”后检验用户名和密码是否正确?
    private void Button1_Click(object sender, System.EventArgs e)
    {
    //建立连接
    string MyConnString = "user id=sa;password=;initial catalog=jrsl;data source=(local);connect timeout=30";
    string strSel = "Select * from userinfo";
    SqlConnection MyConn = new SqlConnection(MyConnString);
    SqlCommand MyComm = new SqlCommand(strSel,MyConn);
    MyComm.Connection.Open(); //读取数据
    string NameString;
    string PString;
    SqlCommand cmd=new SqlCommand("SELECT USERID,PASSWORD FORM USERINFO");
    SqlDataReader dr=cmd.ExecuteReader();

    //获得用户名和密码
    If(dr.Read())
    {
    NameString=dr.GetName(1);
    PString=dr.GetName (2);
    } //检验用户名和密码是否相同
    }
      

  4.   

    你的Button1_Click中的SqlCommand cmd=...,cmd没有指定数据连接,
    asp.net和asp不一样的,在asp中可以打开页面就打开数据连接,数据处理完了之后再关,但是在asp.net中就要用的时候打开,用完及时关闭
    private void Button1_Click(object sender, System.EventArgs e)
    {
       string MyConnString = "user id=sa;password=;initial catalog=jrsl;data source=(local);connect timeout=30";
    string strSel = "Select * from userinfo";
    SqlConnection MyConn = new SqlConnection(MyConnString);
             MyConn.Open();
             SqlCommand cmd=new SqlCommand("SELECT USERID,PASSWORD FORM USERINFO",
    MyConn);
    SqlDataReader dr=cmd.ExecuteReader();
    ...//处理数据
             MyConn.Close();  //记得关闭连接
    }
    这样就可以了
      

  5.   

    谢谢,我以为在Page_Load()开了连接就可以了。
      

  6.   

    SqlCommand cmd=new SqlCommand("SELECT USERID,PASSWORD FORM USERINFO");
    里面没有Connection对象!
    应该好好了解一下变量的作用范围了!