登陆界面
代码的实现:
最上面using添加:
using System.Data.OleDb;
namespace IDmsg
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
OleDbConnection oleDb = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = msg.mdb");//连接数据库语句
登陆button中的事件:
try //用try捕获连接数据库可能出现的错误
{
if (User.Text.Trim() == "" || Passwd.Text.Trim() == "")
{
MessageBox.Show("请输入账号密码!", "Warning!");
}
else
{
oleDb.Open();
string sql = "select * from Users where user='" + User.Text.Trim() + "' and passwd='" + Passwd.Text.Trim() + "'"; //查询数据库该账号密码是否存在
OleDbCommand comm = new OleDbCommand(sql, oleDb);
if (1 != comm.ExecuteNonQuery())
{
Main m1=new Main();
this.Hide();
m1.toolStripStatusLabel4.Text = User.Text.Trim();
m1.Show();
}
oleDb.Close();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message); //打印错误信息
oleDb.Close();
}
取消button中的事件:
Application.Exit();