C# access简单的学生管理系统,登录账号:Admin 密码:123
private void button4_Click(object sender, EventArgs e) { string Con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.mdb";//第二个参数为文件的路径 OleDbConnection dbconn = new OleDbConnection(Con); dbconn.Open();//建立连接 string s = "'" textBox1.Text "'";//接受textBox1的字符串 string Select = "SELECT *FROM student WHERE 姓名 = " s; //select *from 表名 where 字段名='字段值';*表示全表,从全表中 OleDbDataAdapter inst = new OleDbDataAdapter(Select, dbconn);//只匹配满足条件的行 DataSet ds = new DataSet();//临时存储 inst.Fill(ds);//用inst填充ds dataGridView1.DataSource = ds.Tables[0];//展示ds第一张表到dataGridView1控件 dbconn.Close();//关闭连接 } private void button3_Click(object sender, EventArgs e) { string Con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.mdb";//第二个参数为文件的路径 DataTable mytable = myDataSet.Tables["student"]; OleDbConnection dbconn = new OleDbConnection(Con); dbconn.Open();//建立连接 string s = "'" textBox1.Text "'", x = "'" textBox5.Text "'";// 接受textBox的字符串 string Update = "UPDATE student SET 姓名=" x "WHERE 姓名 = " s; string i = "'" textBox1.Text "'", j = "'" textBox5.Text "'";// 接受textBox的字符串 //update 表名 set 字段名='字段值' where 字段值='字段值';上一行代码执行后将所有studentName中的s替换为x OleDbCommand myCommand = new OleDbCommand(Update, dbconn);//执行命令 myCommand.ExecuteNonQuery();//更新数据库,返回受影响行数;可通过判断其是否>0来判断操作是否成功 OleDbDataAdapter inst = new OleDbDataAdapter("SELECT *FROM student", dbconn);//选择全部内容 DataSet ds = new DataSet();//临时存储 inst.Fill(ds);//用inst填充ds dataGridView1.DataSource = ds.Tables[0];//展示ds第一张表到dataGridView1控件 dbconn.Close();//关闭连接 } private void button2_Click(object sender, EventArgs e) { string Con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.mdb";//第二个参数为文件的路径 OleDbConnection dbconn = new OleDbConnection(Con); dbconn.Open();//建立连接 string s = "'" textBox1.Text "'";//接受textBox1的字符串 string Delete = "DELETE FROM student WHERE 姓名 = " s; //delete from 表名 where 字段名='字段值';以上代码执行后会将所有studentName为textbox中内容的行删除 OleDbCommand myCommand = new OleDbCommand(Delete, dbconn);//执行命令 myCommand.ExecuteNonQuery();//更新数据库,返回受影响行数;可通过判断其是否>0来判断操作是否成功 OleDbDataAdapter inst = new OleDbDataAdapter("SELECT *FROM student", dbconn);//选择全部内容 DataSet ds = new DataSet();//临时存储 inst.Fill(ds);//用inst填充ds dataGridView1.DataSource = ds.Tables[0];//展示ds第一张表到dataGridView1控件 dbconn.Close();//关闭连接 } private void button1_Click(object sender, EventArgs e) { string Con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.mdb";//第二个参数为文件的路径 OleDbConnection dbconn = new OleDbConnection(Con); dbconn.Open();//建立连接 string Insert = "INSERT INTO student(姓名,年龄,身高,性别) values('" textBox1.Text "','" textBox2.Text "','" textBox3.Text "','" textBox4.Text "')"; //insert into 表名(字段1,字段2...)values('字段一内容','字段二内容'),上一行 用于字符串的连接,如果想用textBox传值,可用 //string s = "'" textBox1.Text "'", x = "'" textBox2.Text "'"; OleDbCommand myCommand = new OleDbCommand(Insert, dbconn);//执行命令 myCommand.ExecuteNonQuery();//更新数据库,返回受影响行数;可通过判断其是否>0来判断操作是否成功 OleDbDataAdapter inst = new OleDbDataAdapter("SELECT *FROM student", dbconn);//选择全部内容 DataSet ds = new DataSet();//临时存储 inst.Fill(ds);//用inst填充ds dataGridView1.DataSource = ds.Tables[0];//展示ds第一张表到dataGridView1控件 dbconn.Close();//关闭连接 } private void button5_Click(object sender, EventArgs e) { Application.Exit(); } private void textBox1_TextChanged(object sender, EventArgs e) { } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void Form2_Load(object sender, EventArgs e) { } private void textBox1_TextChanged_1(object sender, EventArgs e) { } }}
评论