ASP.NET+Access
一、登入部分
主界面 Fm = new 主界面();
string s1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=student.mdb";
string s2 = "select*from 登录信息表 where 用户名='" + this.ttbName.Text + "' and 密码 ='" + ttbPwd.Text + "'and 身份='" + cbbId.Text + "'";
if (ttbName.Text == "" || ttbName.Text == "")
{
MessageBox.Show("信息不完整,请输入密码和用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
ttbName.Clear();
ttbPwd.Clear();
ttbName.Focus();
return;
}
if (cbbId.Text == "")
{
MessageBox.Show("信息错误,请选择身份", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
OleDbConnection cn = new OleDbConnection(s1);
cn.Open();//打开数据库
OleDbCommand cm = new OleDbCommand(s2, cn);
OleDbDataReader dr = cm.ExecuteReader();//执行oledatacommand命令,
if (dr.HasRows)
{
while (dr.Read())
{
string strname = dr["用户名"].ToString().Trim();
string username = this.ttbName.Text;
string shenfen = dr["身份"].ToString().Trim();
if (username == strname)
{
string strmima = dr["密码"].ToString().Trim();
string mima = this.ttbPwd.Text;
if (strmima == mima)
{
if (shenfen == this.cbbId.Text)
{
Fm.UserName = strname;
Fm.PassWord = strmima;
Fm.gly = shenfen;
Fm.Show();
ttbName.Text = "";
ttbPwd.Text = "";
cbbId.Text = "";
this.Hide();
}
}
}
}
}
else
{
MessageBox.Show("对不起,输入错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
ttbName.Text = "";
ttbPwd.Text = "";
cbbId.Text = "";
ttbName.Focus();
}
cn.Close();
二、查询部分
string s1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=student.mdb";
string strSql = "select * from 学生信息表";
FindValue = ""; //清空存储查询语句的变量
string Find_SQL = strSql; //存储显示数据表中所有信息的SQL语句
if (ttbName.Text == "" && ttbNumber.Text == "" && ttbSex.Text == "" && ttbSch.Text == "" && ttbPlace.Text == "" && ttbBrith.Text == "")
{
OleDbConnection cn;
cn = new OleDbConnection(s1);
cn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(strSql, cn);
DataSet ds = new DataSet();
da.Fill(ds, "学生信息表");
dataGridView1.DataSource = ds.Tables[0];
cn.Close();
}
else
{
if (FindValue.Length > 0)
FindValue = FindValue + "and";
if (ttbNumber.Text != "")
FindValue += "(学号='" + ttbNumber.Text + "') and";
if (ttbName.Text != "")
FindValue += "(姓名='" + ttbName.Text + "') and";
if (ttbPlace.Text != "")
FindValue += "(籍贯='" + ttbPlace.Text + "') and";
if (ttbSex.Text != "")
FindValue += "(性别='" + ttbSex.Text + "') and";
if (ttbSch.Text != "")
FindValue += "(院系='" + ttbSch.Text + "') and";
if (ttbBrith.Text != "")
FindValue += "(生日='" + ttbBrith.Text + "') and";
if (FindValue.Length > 0) //当存储查询条件的变量不为空时,删除逻辑运算符AND
{
if (FindValue.IndexOf("and") > -1) //判断是否用AND连接条件
FindValue = FindValue.Substring(0, FindValue.Length - 4);
}
else
FindValue = "";
if (FindValue != "") //如果FindValue字段不为空
//将查询条件添加到SQL语句的尾部
Find_SQL = Find_SQL + " where " + FindValue;
//按照指定的条件进行查询
OleDbConnection cn;
cn = new OleDbConnection(s1);
cn.Open();
OleDbDataAdapter da = new OleDbDataAdapter(Find_SQL, cn);
DataSet ds = new DataSet();
da.Fill(ds, "信息表");
dataGridView1.DataSource = ds.Tables[0];
cn.Close();
}
}
private void btnCal_Click(object sender, EventArgs e)
{
this.Close();
}
三、修改删除部分
public partial class 用户管理 : Form
{
public 用户管理()
{
InitializeComponent();
panel1.Hide();
}
public void f()
{
cbbId.Items.Clear();
string s1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=student.mdb";
string s2 = "select 用户名 from 登录信息表 ";
OleDbConnection cn = new OleDbConnection(s1);
cn.Open();
OleDbCommand cm = new OleDbCommand(s2, cn);
OleDbDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
cbbId.Items.Add(dr["用户名"].ToString().Trim());
}
cn.Close();
}
//检查用户
public bool dgResult_load()
{
string s1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=student.mdb";
string s2 = "select*from 登录信息表 where 用户名='" + ttbUserName.Text + "'";
string userName = "";
OleDbConnection cn = new OleDbConnection(s1);
cn.Open();//打开数据库
OleDbCommand cm = new OleDbCommand(s2, cn);
OleDbDataReader dr = cm.ExecuteReader();//执行oledatacommand命令,
while (dr.Read())
{
userName = dr["用户名"].ToString().Trim();
if (userName == ttbUserName.Text)
{
return true;
}
else
{
panel1.Hide();
continue;
}
}
return false;
}
//查询用户是否存在
private void ttbSearch_Click(object sender, EventArgs e)
{
if (ttbUserName.Text=="")
{
MessageBox.Show("输入信息不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
else
{
if (dgResult_load())
{
panel1.Show();
}
}
}
//确定之后录入数据库
private void button5_Click(object sender, EventArgs e)
{
string s1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=student.mdb";
string s2 = "update 登录信息表 set 用户名='" + ttbUserName.Text + "',密码='" + textBox4.Text + "',身份='" + comboBox1.Text + "'where 用户名='" + ttbUserName.Text + "'";
if (textBox4.Text == ""&& comboBox1.Text=="")
{
MessageBox.Show("输入信息不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
OleDbConnection cn = new OleDbConnection(s1);
cn.Open();
OleDbCommand cm = new OleDbCommand(s2, cn);
cm.ExecuteNonQuery();
cn.Close();
MessageBox.Show("信息更改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
ttbUserName.Text = ""; textBox4.Text = ""; comboBox1.Text = "";
}
}
private void button6_Click(object sender, EventArgs e)
{
this.Close();
}
//删除用户
private void btnDel_Click(object sender, EventArgs e)
{
string s1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=student.mdb";
string s2 = "delete * from 登录信息表 where 用户名='" + cbbId.Text + "'";
if (ttbUserName.Text == "")
{
MessageBox.Show("删除用户不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
else
{
if (dgResult_load())
{
OleDbConnection cn = new OleDbConnection(s1);
cn.Open();
OleDbCommand cmd = new OleDbCommand(s2, cn);
cmd.ExecuteNonQuery();
MessageBox.Show("用户删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
ttbUserName.Text = "";
panel1.Hide();
}
else
{
MessageBox.Show("该用户不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
ttbUserName.Text = "";
ttbUserName.Focus();
}
}
}
//用户添加的取消
private void btnCal_Click(object sender, EventArgs e)
{
this.Close();
}
//用户添加的OK
private void btnOK_Click(object sender, EventArgs e)
{
if (ttbUName.Text == "")
{
MessageBox.Show("请输入名字!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
ttbUserName.Focus();
return;
}
if (ttbUPwd.Text == "")
{
MessageBox.Show("请输入密码!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
ttbUPwd.Focus();
return;
}
if(cbbId.Text=="")
{
MessageBox.Show("请输入身份!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
cbbId.Focus();
return;
}
string s1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=student.mdb";
string s2 = "insert into 登录信息表 (用户名,密码,身份) values('" + ttbUName.Text + "','" + ttbUPwd.Text + "','" + cbbId.Text + "')";
OleDbConnection cn = new OleDbConnection(s1);
cn.Open();
OleDbCommand cm = new OleDbCommand(s2, cn);
cm.ExecuteNonQuery();
cn.Close();
MessageBox.Show("用户添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
ttbUName.Text = ""; ttbUPwd.Text = ""; cbbId.Text = "";
}
}
详细源码及注释:http://download.csdn.net/detail/s10141303/5649101
本文转自蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366784,如需转载请自行联系原作者