DAL数据库学习(2)
通过数据库连接有很多好处。虽然对于现在的各种存储结构了解不多,但是凭我现在的认知来看,数据库是现在乃至未来的“潮流”,完全引领着数据存储的走向。
我在原来所做测试的基础上按照网上及书上的方法进行了新的测试(在窗口模式下),代码如下
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { comboBox1.Text = "项目组员"; string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;"; strConnection += @"Data Source=Database.accdb"; OleDbConnection objConnection = new OleDbConnection(strConnection); objConnection.Open(); string a = listBox1.SelectedValue.ToString(); OleDbCommand cmm = new OleDbCommand("select * from 项目组员 where User ='" + a + "'", objConnection); OleDbDataReader coo = cmm.ExecuteReader(); while (coo.Read()) { textBox1.Text = coo["Code"].ToString(); textBox2.Text = coo["Realname"].ToString(); textBox3.Text = coo["Birthday"].ToString(); textBox4.Text = coo["Joindate"].ToString(); textBox5.Text = ""; textBox6.Text = coo["Grade"].ToString(); textBox7.Text = coo["User"].ToString(); } coo.Close(); } private void button1_Click(object sender, EventArgs e) { string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;"; strConnection += @"Data Source=Database.accdb"; OleDbConnection objConnection = new OleDbConnection(strConnection); objConnection.Open(); if (textBox5.Text == textBox1.Text) { OleDbCommand comm = new OleDbCommand("update "+comboBox1.Text+" set Code='" + textBox1.Text + "' where User='" + textBox7.Text + "'", objConnection); comm.ExecuteReader(); comm = new OleDbCommand("update 项目信息 set Realname='" + textBox2.Text + "'where User='" + textBox7.Text + "'", objConnection); comm.ExecuteReader(); comm = new OleDbCommand("update " + comboBox1.Text + " set Birthday='" + textBox3.Text + "'where User='" + textBox7.Text + "'", objConnection); comm.ExecuteReader(); comm = new OleDbCommand("update " + comboBox1.Text + " set Joindate='" + textBox4.Text + "' where User='" + textBox7.Text + "'", objConnection); comm.ExecuteReader(); comm = new OleDbCommand("update " + comboBox1.Text + " set Grade='" + textBox6.Text + "' where User='" + textBox7.Text + "'", objConnection); comm.ExecuteReader(); MessageBox.Show("保存成功!"); objConnection.Close(); } else { MessageBox.Show("确认密码错误,请再次输入!"); } objConnection.Close(); } private void button2_Click(object sender, EventArgs e) { comboBox1.Text = "项目管理员"; string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;"; strConnection += @"Data Source=Database.accdb"; OleDbConnection objConnection = new OleDbConnection(strConnection); objConnection.Open(); OleDbCommand cmm = new OleDbCommand("select * from 项目管理员 ", objConnection); OleDbDataReader coo = cmm.ExecuteReader(); while (coo.Read()) { textBox1.Text = coo["Code"].ToString(); textBox2.Text = coo["Realname"].ToString(); textBox3.Text = coo["Birthday"].ToString(); textBox4.Text = coo["Joindate"].ToString(); textBox5.Text = ""; textBox6.Text = coo["Grade"].ToString(); textBox7.Text = coo["User"].ToString(); } coo.Close(); } private void button3_Click(object sender, EventArgs e) { comboBox1.Text = "总经理"; string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;"; strConnection += @"Data Source=Database.accdb"; OleDbConnection objConnection = new OleDbConnection(strConnection); objConnection.Open(); OleDbCommand cmm = new OleDbCommand("select * from 总经理 ", objConnection); OleDbDataReader coo = cmm.ExecuteReader(); while (coo.Read()) { textBox1.Text = coo["Code"].ToString(); textBox2.Text = coo["Realname"].ToString(); textBox3.Text = coo["Birthday"].ToString(); textBox4.Text = coo["Joindate"].ToString(); textBox5.Text = ""; textBox6.Text = coo["Grade"].ToString(); textBox7.Text = coo["User"].ToString(); } coo.Close(); }
UI界面如图:
通过与数据库的连接 就可以实现用户信息的修改了。
明天将开始对网站添加数据库测试量。