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.OleDb;//连接Access数据库必须
namespace //随便
{
public partial class Login : Form//登陆窗体
{
public Login()//构造函数
{
InitializeComponent();
}
public bool blCanLogin;//设置参数如果为true,就登陆,否则放弃登陆
OleDbConnection connection1 = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = C:\\db1.mdb");//连接数据库db1.mdb 在c盘根目录
private void button1_Click(object sender, EventArgs e)
//定义登陆按钮事件
{
blCanLogin = false;//初始值为false
string sql = "select name,passwd from userinfo";//从userinfo表中查询name(用户名)和passwd(密码)
DataRow myRow;
connection1.Open();
OleDbDataAdapter adp = new OleDbDataAdapter(sql, connection1);//查询
DataSet ds = new DataSet();//定义数据集
adp.Fill(ds, "user");//填充数据集
if (textBox1.Text.Trim() != "")
{
if (textBox2.Text.Trim() != "")
{
for (int i = 0; i < ds.Tables["user"].Rows.Count; i++)
{
myRow = ds.Tables["user"].Rows[i];
if (myRow[0].ToString().Trim() == textBox1.Text.ToString().Trim() && myRow[1].ToString().Trim() == textBox2.Text.ToString().Trim())
{
blCanLogin = true;
this.Close();
return;
}
}
MessageBox.Show("你输入的用户名或密码不正确!");
connection1.Close();
return;
}
else
MessageBox.Show("密码不能为空");
return;
}
else
MessageBox.Show("用户名不能为空");
connection1.Close();
}
private void button2_Click(object sender, EventArgs e)
{
blCanLogin = false;
this.Close();
}
}
}