最近,论坛上有些同仁们想知道怎么编写登录窗体。由于斑竹门比较忙碌,我做小弟的愿意为他们效劳!!
下面是我最近几天刚弄好的代码,它能够判断 是否输入了用户名和密码  以及  输入的是否真确!!
当然还能加入“连续登录错误  N  次  系统自动关闭”以及其他的一些功能,谁想加入的可以互相讨论啊
现在共享一下:
  private void button1_Click(object sender, EventArgs e)//button1的Text是:登录
        {
            
            if (this.textBox1.Text == "")
            {
                MessageBox.Show("用户名不能为空!", "错误");
            }
            else if (this.textBox2.Text == "")
            {
                MessageBox.Show("密码不能为空!", "错误");
            }
            else
            {
                if (IsUser(this.textBox1.Text))
                {
                    if (this.textBox2.Text == LoginUser(this.textBox1.Text))
                    {
                        Form2 frm2 = new Form2();
                        // //frm2.showdialog();
                        frm2.Show();
                       Close();
                    }
                    else
                    {
                        MessageBox.Show("密码错误!");
                    }
                }
                else
                {
                    MessageBox.Show("用户名错误!");
                }
            }
        }
        private string LoginUser(string uName) //检测用户是否能够登录
        {
            DataSet ds = new DataSet();
            string u = uName;
            OleDbConnection conn = new OleDbConnection("Data Source=F:\\c#.net\\学生信息管理\\denglu.mdb;provider=Microsoft.Jet.OLEDB.4.0");//连接数据库,我用的是Access
            OleDbDataAdapter daAuthors = new OleDbDataAdapter("Select * From denglubiao where userName='" + uName + "'", conn);
            conn.Open();
            daAuthors.Fill(ds, "denglubiao");
            
           return ds.Tables[0].Rows[0]["userPassWD"].ToString();
           //SqlParameter parName = new SqlParameter("@Name", SqlDbType.VarChar, 50);
          // parName.Value = User;
           OleDbParameter parName = new OleDbParameter("@Name", OleDbType.VarChar, 50);
            conn.Close(); 
        }
        private bool IsUser(string uName) //检测用户输入的用户名是否存在
        {
            DataSet ds = new DataSet();
          
            OleDbConnection conn = new OleDbConnection("Data Source=F:\\c#.net\\学生信息管理\\denglu.mdb;provider=Microsoft.Jet.OLEDB.4.0");//连接数据库
            OleDbDataAdapter daAuthors = new OleDbDataAdapter("Select userPassWD From denglubiao where userName='" + uName + "'", conn);
            conn.Open();
            daAuthors.Fill(ds, "denglubiao");
            int n;
            n = ds.Tables[0].Rows.Count;
            if (n > 0)
                return true;//存在就返回true
            else
                return false;
            conn.Close(); 
        }
        private void button2_Click(object sender, EventArgs e)//button2的 Text属性是:取消登录
        {
            Application.Exit();//用户取消登录,退出系统
        }
感觉好的,不妨顶顶啊,多谢了!!
[此贴子已经被作者于2007-3-16 11:05:22编辑过]



 
											






 
	    

 
	




 
										
					
	