using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OracleConnection conn;//声明一个OracleConnection变量
OracleDataAdapter adapter;
DataSet ds;
OracleDataAdapter sda;
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//实例化OracleConnection变量conn,连接数据库
conn = new OracleConnection("Data Source=网络服务名;Persist Security Info=True;User ID=***;Password=***;Unicode=True");
//创建一个OracleDataAdapter对象sda
sda = new OracleDataAdapter("select * from T_table2", conn);
//创建一个DataSet对象
ds = new DataSet();
//使用OracleDataAdapter对象的Fill方法填充DataSet
sda.Fill(ds, "T_table2");
//设置dataGridView1控件数据源
dataGridView1.DataSource = ds.Tables[0];
//button1.Enabled = false;//可以使该按钮操作后不可用,适用于一次行为
//dataGridView1.Columns[0].ReadOnly =false;
}
private DataTable dbconn(string strSql)
{
conn.Open();
this.adapter = new OracleDataAdapter(strSql, conn);
DataTable dtSelect = new DataTable();
int rnt = this.adapter.Fill(dtSelect);
conn.Close();
return dtSelect;
}
private void button_modify_Click(object sender, EventArgs e)
{
conn = new OracleConnection("Data Source=网络服务名;Persist Security Info=True;User ID=***;Password=***;Unicode=True");
conn.Open();
//update table2 set field1=value1 where 范围
string sql = "update T_table2 set " + textBox_modifyType.Text + "='" + textBox_nameAfter.Text + "'where " + textBox_modifyType.Text + "= '" + textBox_nameBefore.Text + "'";
//string sql = "update T_table2 set name='" + textBox_nameAfter.Text + "'where name= '" + textBox_nameBefore.Text + "'";
OracleCommand cmd = new OracleCommand(sql, conn);
int result = cmd.ExecuteNonQuery();
if (result !=0)
{
MessageBox.Show("修改成功!");
}
else
{
MessageBox.Show("修改失败!");
}
dataGridView1.DataSource = Display();
}
private void button_delete_Click(object sender, EventArgs e)
{
conn = new OracleConnection("Data Source=网络服务名;Persist Security Info=True;User ID=***;Password=***;Unicode=True");
conn.Open();
int i = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
string sql = "delete from T_table2 where ID='" + i + "'";
//this.textBox_id.Text = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
//string sql = "delete from T_table2 where ID='" + textBox_id.Text + "'";
OracleCommand cmd = new OracleCommand(sql, conn);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
MessageBox.Show("删除成功!");
}
else
{
MessageBox.Show("删除失败!");
}
dataGridView1.DataSource = Display();
}
private void button_add_Click(object sender, EventArgs e)
{
conn = new OracleConnection("Data Source=网络服务名;Persist Security Info=True;User ID=***;Password=***;Unicode=True");
conn.Open();
string sql = "insert into T_table2(ID,NAME,AGE,SEX,SCHOOL) values('" + textBox_id.Text + "','" + textBox_name.Text + "','" + textBox_age.Text+ "','"+ textBox_sex.Text + "','" + textBox_school.Text + "')";
OracleCommand cmd = new OracleCommand(sql, conn);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
MessageBox.Show("添加成功!");
}
else
{
MessageBox.Show("添加失败!");
}
dataGridView1.DataSource = Display();
}
private DataTable Display()
{
conn = new OracleConnection("Data Source=网络服务名;Persist Security Info=True;User ID=***;Password=***;Unicode=True");
conn.Open();
string sql = "select * from T_table2";
OracleCommand cmd = new OracleCommand(sql, conn);
OracleDataAdapter oda = new OracleDataAdapter(cmd);
DataTable dt = new DataTable();
oda.Fill(dt);
conn.Close();
cmd.Dispose();
return dt;
}
private void button_select_Click(object sender, EventArgs e)
{
conn = new OracleConnection("Data Source=网络服务名;Persist Security Info=True;User ID=***;Password=***;Unicode=True");
sda = new OracleDataAdapter("select * from T_table2 where name='" + this.textBox_name.Text + "' or sex ='" + textBox_sex.Text + "'", conn);
ds = new DataSet();
sda.Fill(ds, "T_table2");
//设置dataGridView1控件数据源
dataGridView1.DataSource = ds.Tables[0];
}
private void textBox_modifyType_TextChanged(object sender, EventArgs e)
{
}
}
}
运行效果:
Oracle数据库管理系统
设置选择数据库记录方式dataGridViews—SelectionMode
转载于:https://blog.51cto.com/yuanjianliang/1324997