更新学生信息
删除学生信息
学生表增加、删除、修改代码(部分)
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Data.OracleClient; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms;
namespace MyProgram {
public partial class Form3 : Form {
private string ConnectionString = \ \ private OracleConnection conn = null; private OracleDataAdapter DataAdapter = null; private DataSet dataset = null;
// private OracleCommand command= null;
public Form3() {
InitializeComponent(); }
private void Form3_Load(object sender, EventArgs e) {
conn = new OracleConnection(ConnectionString); showData();
}
private void showData() {
string tname = \ try {
if (conn == null) conn.Open();
DataAdapter = new OracleDataAdapter(\ dataset = new DataSet(); DataAdapter.Fill(dataset);
dataGridView1.DataSource = dataset;
dataGridView1.DataMember = dataset.Tables[0].ToString();
tname = dataset.Tables[0].ToString(); //先清除所有绑定,然后再重新绑定 textBox1.DataBindings.Clear(); textBox2.DataBindings.Clear(); textBox3.DataBindings.Clear(); textBox4.DataBindings.Clear(); textBox5.DataBindings.Clear(); textBox6.DataBindings.Clear();
textBox1.DataBindings.Add(\ textBox2.DataBindings.Add(\ textBox3.DataBindings.Add(\ textBox4.DataBindings.Add(\ textBox5.DataBindings.Add(\ textBox6.DataBindings.Add(\ }
catch (Exception ex) {
MessageBox.Show(ex.ToString()); } }
private void button1_Click(object sender, EventArgs e) {
string strOracle = \ strOracle += \学号 strOracle += \姓名 strOracle += \学院 strOracle += \班级 strOracle += \年龄 strOracle += \性别 OracleCommand command = null; try {
command = new OracleCommand(); command.Connection = conn; command.CommandText = strOracle; conn.Open();
int n = command.ExecuteNonQuery();//执行Insert语句 if (n > 0)
MessageBox.Show(\成功插入数据!\ }
catch (Exception ex)
{
MessageBox.Show(ex.Message); }
finally {
if (conn != null) conn.Close(); command.Dispose(); }
showData(); }
private void button2_Click(object sender, EventArgs e) {
Form7 f7 = new Form7(); f7.Show();
}
private void button3_Click(object sender, EventArgs e) { try {
OracleCommandBuilder builder = new OracleCommandBuilder(DataAdapter); int n = DataAdapter.Update(dataset, \
MessageBox.Show(\成功更新数据,有\行受到更新!\ }
catch {
MessageBox.Show(\更新不成功!\ } }
private void button4_Click(object sender, EventArgs e) {
string curNo = \
if (dataGridView1.Rows.Count <= 1) return;
int index = dataGridView1.CurrentRow.Index;
dataGridView1.Rows[index].Selected = true;
curNo = this.dataGridView1.Rows[index].Cells[0].Value.ToString(); OracleCommand command = null;
string strOracle = \
try {
command = new OracleCommand(); command.Connection = conn; command.CommandText = strOracle; conn.Open();
int n = command.ExecuteNonQuery(); }
catch (Exception ex) {
MessageBox.Show(ex.Message); }
finally {
if (conn != null) conn.Close(); command.Dispose(); }
showData();
MessageBox.Show(\成功删除一行!\ } } }
选择检索条件(学生表查询)