【实例简介】C# 实现 Oracle 数据库的 增删改查 操作
【实例截图】
【核心代码】
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.OleDb;
using System.Data.OracleClient;
namespace Oracle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
allgoods();
}
//提取商品信息
public void allgoods()
{
try
{
OracleConnection con = new OracleConnection("Data Source=orcl;User ID=SA;password=123;");
OracleDataAdapter da = new OracleDataAdapter();
OracleCommand cmd = new OracleCommand();
cmd.Connection = con;
cmd.CommandText = "ALLGOODS";
OracleParameter param_out = cmd.Parameters.Add("P_CURSOR", OracleType.Cursor);
param_out.Direction = ParameterDirection.Output;
cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
addgoods a = new addgoods();
a.Show();
allgoods();
}
private void button3_Click(object sender, EventArgs e)
{
if (MessageBox.Show("真的要删除吗", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
return;
if (dataGridView1.Rows.Count > 0)
{
OracleConnection con = new OracleConnection("Data Source=orcl;User ID=sa;password=123;");
OracleCommand cmd = new OracleCommand();
con.Open();
cmd.Connection = con;
cmd.CommandText = "DELETEGOODS";
OracleParameter[] parameters ={
new OracleParameter("gid",OracleType.Number),
};
parameters[0].Value = dataGridView1.CurrentRow.Cells["商品编号"].Value.ToString();
parameters[0].Direction = ParameterDirection.Input;
foreach (OracleParameter parameter in parameters)
{
cmd.Parameters.Add(parameter);
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
con.Close();
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
}
}
private void button2_Click(object sender, EventArgs e)
{
xggoods frm = new xggoods (int.Parse (dataGridView1.CurrentRow.Cells[0].Value.ToString()), dataGridView1.CurrentRow.Cells[1].Value.ToString(), dataGridView1.CurrentRow.Cells["供应商"].Value.ToString());
frm.ShowDialog();
allgoods();
}
private void button4_Click(object sender, EventArgs e)
{
try
{
dataGridView1.AutoGenerateColumns = false;
OracleConnection con = new OracleConnection("Data Source=orcl;User ID=SA;password=123;");
OracleDataAdapter da = new OracleDataAdapter();
OracleCommand cmd = new OracleCommand();
cmd.Connection = con;
cmd.CommandText = "SELECTGOODS";
OracleParameter[] parameters ={
new OracleParameter("gname",OracleType.VarChar ),
};
parameters[0].Value = textBox1.Text;
OracleParameter param_out = cmd.Parameters.Add("P_CURSOR", OracleType.Cursor);
param_out.Direction = ParameterDirection.Output;
foreach (OracleParameter parameter in parameters)
{
cmd.Parameters.Add(parameter);
}
cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button5_Click(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
allgoods();
}
}
}