本人刚学c# 想做一个登录框比较textbox的帐号密码和sql是否相等。
但是我不知道怎么写这个1  请问能否给予例子?(代码的)
2  请问sql的查询语句在哪里可以找到?

解决方案 »

  1.   

    注:登录按钮name是button1,两个文本框name已标出。 
    private void button1_Click(object sender, EventArgs e) 

      string ConnStr = "server=.;Integrated Security=SSPI;Database=C;";//数据库连接字符串 
      if (txtUser.Text != "" && txtPwd.Text != "") 
      { 
          SqlConnection myConnection = new SqlConnection(ConnStr); 
          myConnection.Open();//打开连接 
          SqlCommand myCommand = new SqlCommand("select * from user where name='" +  txtUser.Text.Trim() + "' and pwd='" + txtPwd.Text.Trim() + "'", myConnection); 
          SqlDataReader myDataReader = myCommand.ExecuteReader(); 
          bool ifcon = myDataReader.Read(); 
          if (ifcon) 
          { 
            this.Hide();//登录界面隐藏  (当然楼主可以随意,哈哈) 
            frmDataKeep DataKeep = new frmDataKeep();//实例化窗体类frmDataKeep 
            DataKeep.ShowDialog();//登录到name属性为frmDataKeep的窗体 
          } 
          else 
          { 
            MessageBox.Show("登录帐号或登录密码错误!"); 
            txtUser.Text = ""; 
            txtPwd.Text = ""; 
            txtUser.Focus();//txtUser文本框获得焦点 
          } 
            myConnection.Close();//关闭连接 
      } 
      else 
      { 
          MessageBox.Show("登录帐号或登录密码不能为空!"); 
          txtUser.Focus(); 
          return; 
      } 
    } 楼主看这咋样 应该很简单,以上登录SOL Server2000数据库的方式为  Windows身份验证 
    若是采用 SOL Server身份验证,则代码string ConnStr = "server=.;Integrated Security=SSPI;Database=C;"应改为string ConnStr = "server=.;Database =C; uid = sa; pwd = sa";就OK了。 
      

  2.   

    select * from user where name='" +  txtUser.Text.Trim() + "' and pwd='" + txtPwd.Text.Trim() + "'"
      

  3.   

    关于验证用户名我文革小问题
    假设name="abc";input="aa||1"
    这样name==input的值是?
    name.equals(input)呢?