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);
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 = "";
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();
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;
//按照指定的條件進行查詢
OleDbDataAdapter da = new OleDbDataAdapter(Find_SQL, cn);
da.Fill(ds, "資訊表");
}
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 s2 = "select 使用者名 from 登入資訊表 ";
cn.Open();
OleDbDataReader dr = cm.ExecuteReader();
while (dr.Read())
cbbId.Items.Add(dr["使用者名"].ToString().Trim());
//檢查使用者
public bool dgResult_load()
string s2 = "select*from 登入資訊表 where 使用者名='" + ttbUserName.Text + "'";
string userName = "";
userName = dr["使用者名"].ToString().Trim();
if (userName == ttbUserName.Text)
{
return true;
panel1.Hide();
continue;
return false;
//查詢使用者是否存在
private void ttbSearch_Click(object sender, EventArgs e)
if (ttbUserName.Text=="")
MessageBox.Show("輸入資訊不能為空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
if (dgResult_load())
panel1.Show();
//确定之後錄入資料庫
private void button5_Click(object sender, EventArgs e)
string s2 = "update 登入資訊表 set 使用者名='" + ttbUserName.Text + "',密碼='" + textBox4.Text + "',身份='" + comboBox1.Text + "'where 使用者名='" + ttbUserName.Text + "'";
if (textBox4.Text == ""&& comboBox1.Text=="")
OleDbConnection cn = new OleDbConnection(s1);
OleDbCommand cm = new OleDbCommand(s2, cn);
cm.ExecuteNonQuery();
MessageBox.Show("資訊更改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
ttbUserName.Text = ""; textBox4.Text = ""; comboBox1.Text = "";
private void button6_Click(object sender, EventArgs e)
//删除使用者
private void btnDel_Click(object sender, EventArgs e)
string s2 = "delete * from 登入資訊表 where 使用者名='" + cbbId.Text + "'";
if (ttbUserName.Text == "")
MessageBox.Show("删除使用者不能為空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
OleDbConnection cn = new OleDbConnection(s1);
cn.Open();
OleDbCommand cmd = new OleDbCommand(s2, cn);
cmd.ExecuteNonQuery();
MessageBox.Show("使用者删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
ttbUserName.Text = "";
MessageBox.Show("該使用者不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
ttbUserName.Focus();
//使用者添加的取消
//使用者添加的OK
private void btnOK_Click(object sender, EventArgs e)
if (ttbUName.Text == "")
MessageBox.Show("請輸入名字!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
ttbUserName.Focus();
if (ttbUPwd.Text == "")
MessageBox.Show("請輸入密碼!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
ttbUPwd.Focus();
if(cbbId.Text=="")
MessageBox.Show("請輸入身份!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
cbbId.Focus();
string s2 = "insert into 登入資訊表 (使用者名,密碼,身份) values('" + ttbUName.Text + "','" + ttbUPwd.Text + "','" + cbbId.Text + "')";
cm.ExecuteNonQuery();
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,如需轉載請自行聯系原作者