C#连接Oracle数据库详细版(2022版vs)

1 篇文章 0 订阅

1、创建一个窗体

 2、窗体上加上以下几个控件(容器,单选框,文本,文本框,按钮,数据)

左边控件实现增删改查操作,右边数据控件是为了显示数据

 3、添加驱动(重点)

点击引用右键选中【管理NuGet程序包】。

点击浏览输入oracle进行搜索,选中第二个,进行安装。

 4、为了代码更整洁我们再创建一个类(使用类的时候要引用)

 以下是类里的代码,封装了两个方法,方法一实现:查询,读取数据,方法二实现:插入、删除、修改数据。(新手的小伙伴可以直接复制,记得把db变量改成自己的数据库信息)

using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace WindowsFormsApp2.myclass
{
    public class oraclecalss
    {
        //连接数据库的信息
        private static string db = "自己的数据库信息";
        //查询oracle数据库内容返回数据集
        public DataTable oracleTable(string ora)
        {
            //链接数据库
            OracleConnection con = new OracleConnection(db);
            //把数据放入数据集
            DataTable dt = new DataTable();
            try
            {
                //连接数据库
                con.Open();
                //查询数据
                OracleDataAdapter da = new OracleDataAdapter(ora, con);
                //查询道德数据放入数据集里
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                //失败出现异常,原因在ex.message里。
            }
            finally
            {
                //断开数据库
                con.Close();
            }
            //把数据集返回出去
            return dt;
        }
        //修改 删除 添加在oracle数据库中 ,返回一个数值
        public int Execoraint(string ora)
        {

            //初始值等于0
            int row = 0;
            //连接数据库
            OracleConnection con = new OracleConnection(db);
            con.Open();
            try
            {
                //查询数据
                OracleCommand cod = new OracleCommand(ora, con);
                row = cod.ExecuteNonQuery();
            }
            catch (Exception ex) { }
            finally
            {
                con.Close();
            }
            return row;
        }
    }
}

 5、在formoracle.cs窗体中引用此类实现增删改查操作

1、刚进入窗体就显示所有数据

        //引用类 
        //myclass是我所有类的文件夹名,没有嵌套文件夹的语法oraclecalss _ora = new oraclecalss();
        myclass.oraclecalss _ora = new myclass.oraclecalss();
        //我把读取所有数据封装成一个方法了,在需要全部数据时直接调用。
        private void getAllData()
        {
            //ora是Oracle语句
            string ora = "select emp_no,emp_name,emp_tel,emp_email from test.myuser";
            //dataGridView1是你使用数据控件的名字。
            dataGridView1.DataSource = _ora.oracleTable(ora);
        }
        //刚进入窗体时执行的方法。
        private void Formoracle_Load(object sender, EventArgs e)
        {
            this.Text = "Oracle 数据库";
            //进入窗体调用封装的方法显示所有数据。
            getAllData();

        }
1.1效果图

 2、添加数据操作

        //button按钮不同文字对应不同逻辑
        private void button1_Click(object sender, EventArgs e)
        {
            //获取所有文本框的内容
            string empnostr = empno.Text.Trim();
            string empnamestr = empname.Text.Trim();
            string emptelstr = emptel.Text.Trim();
            string empemailstr = empemail.Text.Trim();
            //按钮文字是添加时
            if (button1.Text == "添加")
            {
                //文本框不能为空
                if(empemailstr == "" || emptelstr == "" || empnamestr == "" || empnostr == "")
                {
                    MessageBox.Show("内容不能为空!");
                    return;
                }
                string ora = "insert into test.myuser values(" + empnostr + ",'" + empnamestr + "'," + emptelstr + ",'" + empemailstr + "')";
                int row = _ora.Execoraint(ora);
                if(row > 0)
                {
                    MessageBox.Show("添加成功");
                    //添加成功文本框为空
                    empno.Text = "";
                    empname.Text = "";
                    emptel.Text = "";
                    empemail.Text = "";
                    //添加成功后数据实时更新
                    getAllData();
                }
                else
                {
                    MessageBox.Show("添加失败");
                }

                
            }
2.1效果图

 确定后数据实时更新

 3、查询数据操作

//按钮文字是查询时
else if (button1.Text == "查询")
            {
                string ora = "select emp_no,emp_name,emp_tel,emp_email from test.myuser where emp_no like '%"+ empnostr +"%'";
                dataGridView1.DataSource = _ora.oracleTable(ora);
            }
3.1效果图

 4、修改数据操作(修改和删除需要数据回显)

4.1数据回显
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //判断只有在删除和修改时才能回显
            if (button1.Text == "修改" || button1.Text == "删除")
            {
                //在表格点击事件外面获取单元格索引
                //int index = dataGridView1.CurrentRow.Index;
                //MessageBox.Show("" + index);
                //获取索引
                int index = e.RowIndex;
                if (index >= 0)
                {
                    empno.Text = dataGridView1.Rows[index].Cells[0].Value.ToString();
                    empname.Text = dataGridView1.Rows[index].Cells[1].Value.ToString();
                    emptel.Text = dataGridView1.Rows[index].Cells[2].Value.ToString();
                    empemail.Text = dataGridView1.Rows[index].Cells[3].Value.ToString();
                    
                }
                
            }
            else if (button1.Text == "添加" || button1.Text == "查询")
                {
                    empno.Text = "";
                    empname.Text = "";
                    emptel.Text = "";
                    empemail.Text = "";
                }
        }
4.2修改数据
//按钮文字是修改时 
else if (button1.Text == "修改")
            {
                if (empnostr == "")
                {
                    MessageBox.Show("请选择你要修改的内容!");
                    return;
                }
                string ora = "update test.myuser set emp_name ='" + empname.Text.Trim() + "',emp_tel = " + emptel.Text.Trim()+ ",emp_email ='" + empemail.Text.Trim() + "' where emp_no = " + empno.Text.Trim();
                int row = _ora.Execoraint(ora);
                if (row > 0)
                {
                    MessageBox.Show("修改成功");
                    empno.Text = "";
                    empname.Text = "";
                    emptel.Text = "";
                    empemail.Text = "";
                    getAllData();
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
4.3效果图

 5、删除数据操作

5.1数据回显

和修改回显一样。

5.2删除数据
//按钮文字是删除时 
else if (button1.Text == "删除")
            {
                string ora = "delete test.myuser where  emp_no = " + empno.Text.Trim();
                int row = _ora.Execoraint(ora);
                if (row > 0)
                {
                    MessageBox.Show("删除成功");
                    empno.Text = "";
                    empname.Text = "";
                    emptel.Text = "";
                    empemail.Text = "";
                    getAllData();
                }
                else
                {
                    MessageBox.Show("删除失败");
                }

            }
        }
5.3效果图

一样工号是禁用,以防修改。

 6、为了看起来更清晰,以下是formoracle.cs窗体的整体代码

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;

namespace WindowsFormsApp2.NewFolder1
{
    public partial class Formoracle : Form
    {
        public Formoracle()
        {
            InitializeComponent();
        }
        
        private void label1_Click(object sender, EventArgs e)
        {

        }
        //引用类
        myclass.oraclecalss _ora = new myclass.oraclecalss();
        private void getAllData()
        {
            string ora = "select emp_no,emp_name,emp_tel,emp_email from test.myuser";
            dataGridView1.DataSource = _ora.oracleTable(ora);
        }
        private void Formoracle_Load(object sender, EventArgs e)
        {
            this.Text = "Oracle 数据库";
            //进入页面显示数据
            getAllData();

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            //单选按钮切换button按钮文字
            if (radioButton1.Checked == true)
            {
                radioButton2.Checked = false;
                radioButton4.Checked = false;
                radioButton3.Checked = false;
                button1.Text = "添加";
                empno.ReadOnly = false;

            }
            else if (radioButton2.Checked == true)
            {
                radioButton1.Checked = false;
                radioButton3.Checked = false;
                radioButton4.Checked = false;
                button1.Text = "查询";
                empno.ReadOnly = false;
            }
            else if (radioButton3.Checked == true)
            {
                radioButton1.Checked = false;
                radioButton4.Checked = false;
                radioButton2.Checked = false;
                button1.Text = "修改";
                empno.ReadOnly = true;
            }
            else if (radioButton4.Checked == true)
            {
                radioButton2.Checked = false;
                radioButton3.Checked = false;
                radioButton1.Checked = false;
                button1.Text = "删除";
                empno.ReadOnly = true;
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }
       //button按钮不同文字对应不同逻辑
        private void button1_Click(object sender, EventArgs e)
        {
            //获取所有文本框的内容
            string empnostr = empno.Text.Trim();
            string empnamestr = empname.Text.Trim();
            string emptelstr = emptel.Text.Trim();
            string empemailstr = empemail.Text.Trim();
            if (button1.Text == "添加")
            {
                if(empemailstr == "" || emptelstr == "" || empnamestr == "" || empnostr == "")
                {
                    MessageBox.Show("内容不能为空!");
                    return;
                }
                string ora = "insert into test.myuser values(" + empnostr + ",'" + empnamestr + "'," + emptelstr + ",'" + empemailstr + "')";
                int row = _ora.Execoraint(ora);
                if(row > 0)
                {
                    MessageBox.Show("添加成功");
                    empno.Text = "";
                    empname.Text = "";
                    emptel.Text = "";
                    empemail.Text = "";
                    getAllData();
                }
                else
                {
                    MessageBox.Show("添加失败");
                }

                
            }
            else if (button1.Text == "查询")
            {
                string ora = "select emp_no,emp_name,emp_tel,emp_email from test.myuser where emp_no like '%"+ empnostr +"%'";
                dataGridView1.DataSource = _ora.oracleTable(ora);
            }
            else if (button1.Text == "修改")
            {
                if (empnostr == "")
                {
                    MessageBox.Show("请选择你要修改的内容!");
                    return;
                }
                string ora = "update test.myuser set emp_name ='" + empname.Text.Trim() + "',emp_tel = " + emptel.Text.Trim()+ ",emp_email ='" + empemail.Text.Trim() + "' where emp_no = " + empno.Text.Trim();
                int row = _ora.Execoraint(ora);
                if (row > 0)
                {
                    MessageBox.Show("修改成功");
                    empno.Text = "";
                    empname.Text = "";
                    emptel.Text = "";
                    empemail.Text = "";
                    getAllData();
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
            else if (button1.Text == "删除")
            {
                string ora = "delete test.myuser where  emp_no = " + empno.Text.Trim();
                int row = _ora.Execoraint(ora);
                if (row > 0)
                {
                    MessageBox.Show("删除成功");
                    empno.Text = "";
                    empname.Text = "";
                    emptel.Text = "";
                    empemail.Text = "";
                    getAllData();
                }
                else
                {
                    MessageBox.Show("删除失败");
                }

            }
        }

        private void empname_TextChanged(object sender, EventArgs e)
        {

        }

        private void empemail_TextChanged(object sender, EventArgs e)
        {

        }

        private void empno_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //判断只有在删除和修改时才能回显
            if (button1.Text == "修改" || button1.Text == "删除")
            {
                //在表格点击事件外面获取单元格索引
                //int index = dataGridView1.CurrentRow.Index;
                //MessageBox.Show("" + index);
                //获取索引
                int index = e.RowIndex;
                if (index >= 0)
                {
                    empno.Text = dataGridView1.Rows[index].Cells[0].Value.ToString();
                    empname.Text = dataGridView1.Rows[index].Cells[1].Value.ToString();
                    emptel.Text = dataGridView1.Rows[index].Cells[2].Value.ToString();
                    empemail.Text = dataGridView1.Rows[index].Cells[3].Value.ToString();
                    
                }
                
            }
            else if (button1.Text == "添加" || button1.Text == "查询")
                {
                    empno.Text = "";
                    empname.Text = "";
                    emptel.Text = "";
                    empemail.Text = "";
                }
        }
    }
}

以上就实现了连接oracle数据库并操作数据增删改查效果,还希望能够帮助到小伙伴们

  • 13
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
C#连接Oracle数据库可以使用Oracle Data Provider for .NET(ODP.NET)来实现。ODP.NET是Oracle官方提供的用于与Oracle数据库进行交互的.NET数据访问组件。 以下是连接Oracle数据库的基本步骤: 1. 首先,确保你的计算机上已经安装了Oracle客户端软件。你可以从Oracle官方网站下载并安装适合你操作系统的客户端软件。 2. 在C#项目中,添加对Oracle.DataAccess.dll的引用。这个DLL文件是ODP.NET的核心组件,它包含了与Oracle数据库交互所需的类和方法。 3. 在代码中,使用`using`关键字引入`Oracle.DataAccess.Client`命名空间。 4. 创建一个`OracleConnection`对象,并设置连接字符串。连接字符串包含了连接Oracle数据库所需的信息,如服务器地址、用户名、密码等。 ```csharp string connectionString = "Data Source=your_server_address;User Id=your_username;Password=your_password;"; OracleConnection connection = new OracleConnection(connectionString); ``` 5. 打开数据库连接。 ```csharp connection.Open(); ``` 6. 执行SQL语句或存储过程。可以使用`OracleCommand`对象来执行SQL语句,并通过`ExecuteReader`方法获取查询结果。 ```csharp string sql = "SELECT * FROM your_table"; OracleCommand command = new OracleCommand(sql, connection); OracleDataReader reader = command.ExecuteReader(); while (reader.Read()) { // 处理查询结果 } reader.Close(); ``` 7. 关闭数据库连接。 ```csharp connection.Close(); ``` 这是一个简单的连接Oracle数据库的示例,你可以根据自己的需求进行扩展和修改。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小哆不迷糊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值