日期:2014-05-18 浏览次数:20880 次
private void btnOK_Click(object sender, EventArgs e) { if (ValidLogin(txtUsername.Text.Trim(), txtPassword.Text.Trim())) { FrmMain f = new FrmMain(this); f.Show(); this.Hide(); } else { MessageBox.Show("错误", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } private bool ValidLogin(string username, string password) { //数据库放到项目目录/bin/debug 目录下 bool result = false; OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=your.mdb"); string strSql = "select 1 from Users where uid = @uid and pwd = @pwd"; OleDbCommand cmd = new OleDbCommand(strSql, cn); cmd.Parameters.AddWithValue("@uid", txtUsername.Text); cmd.Parameters.AddWithValue("@pwd", txtPassword.Text); cn.Open(); OleDbDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { result = true; } cn.Close(); return result; } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); }