using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using System.Data.OleDb;namespace MealRecordSystem{
public partial class Login : Form
{
string source = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Application.StartupPath + @"\\MealRecord.mdb";//连接数据库
public Login()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();//点击推出按钮关闭窗口
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (this.txtUser.Text.Trim() == "")
{
MessageBox.Show("请输入用户名!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtUser.Focus();
return;
}
if (this.txtpwd.Text.Trim() == "")
{
MessageBox.Show("请输入密码!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.txtpwd.Focus();
return;
}
string userName = txtUser.Text.ToString();
string pwd = txtpwd.Text.ToString();
string select = "SELECT adminPwd FROM Login WHERE adminName= '" + userName + "'";
OleDbConnection Sqlconn = new OleDbConnection(source);
try
{
Sqlconn.Open();
OleDbCommand Sqlcmd = new OleDbCommand(select, Sqlconn);
OleDbDataReader reader = Sqlcmd.ExecuteReader();
if (reader.Read())
{
if (pwd == reader["adminPwd"].ToString().Trim())
{
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("密码不正确","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
txtpwd.SelectAll();
txtpwd.Focus();
}
}
else
{
MessageBox.Show("用户名不存在", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtUser.SelectAll();
txtUser.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void txtpwd_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Return)
SendKeys.SendWait("{TAB}");
}
private void txtUser_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Return)
SendKeys.SendWait("{TAB}");
}
}}
|