登陆源代码:
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("请输入用户名和密码,然后再登陆");
return;
}
string SQL = "select 类别 from 用户表 where 用户名=";
SQL += "'" + textBox1.Text + "'" + "and 密码='" + textBox2.Text + "'";
string myconstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|db1.mdb";
OleDbCommand mycom = null;
OleDbConnection mycon = null;
try
{
mycon = new OleDbConnection(myconstr);
mycon.Open();
mycom = new OleDbCommand(SQL, mycon);
OleDbDataReader rd = mycom.ExecuteReader();
if (rd.HasRows)
{
rd.Read();
this.Hide();
Form1 m = new Form1();
m.ShowDialog();
this.textBox1.Clear();
this.textBox2.Clear();
this.textBox1.Focus();
}
else
{
MessageBox.Show("没有这个用户或密码不正确,请重新登录!");
return;
}
}
catch (OleDbException oe)
{
MessageBox.Show(oe.Message, "Error");
}
finally
{
if (mycon.State == ConnectionState.Open)
mycon.Close();
}
}
注册源代码:
string ConStr ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:DB.mdb";
OleDbConnection cn = new OleDbConnection(ConStr);
try
{
cn.Open();
string insert = "insert into 用户表(userid,psw)values";
insert += "('" + textBox1.Text + "','" + textBox2.Text + "')";
OleDbCommand cm = new OleDbCommand(insert, cn);
DataSet mydata = new DataSet();
OleDbDataAdapter myadapter = new OleDbDataAdapter(cm);
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("所有项都是必填项,请填完后再单击注册按钮");
return;
}
else
myadapter.Fill(mydata, "用户表");
MessageBox.Show("注册成功!");
}
catch (OleDbException te)
{
MessageBox.Show(te.Message);
}
finally
{
if (cn.State == ConnectionState.Open)
cn.Close();
}
}
}
模糊查询源代码:
//按姓名查找
private void button1_Click(object sender, EventArgs e)
{
string ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|db1.mdb;";
OleDbConnection Con = new OleDbConnection(ConStr);
string str = "select * from 学生表 ";
str += "where 姓名 like '%" + textBox1.Text.Trim() + "%'";
OleDbCommand Com = new OleDbCommand(str, Con);
OleDbDataAdapter myda = new OleDbDataAdapter();
myda.SelectCommand = Com;
Con.Open();
DataSet myds = new DataSet();
MessageBox.Show(str);
myda.Fill(myds, "学生表");
Con.Close();
dataGridView1.DataSource = myds;
dataGridView1.DataMember = "学生表";
}