您的位置:首页 > 编程语言 > C#

c#.net用户登录、注册和模糊查询源代码

2010-06-13 14:30 525 查看
登陆源代码:

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();
}
}

注册源代码:

private void button1_Click(object sender, EventArgs e)
{
string ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|db1.mdb;";
OleDbConnection mycon = new OleDbConnection(ConStr);
try
{
mycon.Open();
string insert = "insert into 用户表(用户名,密码,类别)values";
insert += "('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
OleDbCommand mycom = new OleDbCommand(insert,mycon);
DataSet mydata = new DataSet();

OleDbDataAdapter myadapter = new OleDbDataAdapter(mycom);
if (textBox1.Text == "" && textBox2.Text == "")
MessageBox.Show("所有项都是必填项,请填完后再单击注册按钮");
myadapter.Fill(mydata,"用户表");
MessageBox.Show("注册成功!");
//myadapter.Update(mydata, "用户表");
}
catch (OleDbException te)
{
MessageBox.Show(te.Message);
}
finally
{
if (mycon.State == ConnectionState.Open)
mycon.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 = "学生表";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐