C#简单连接Oracle数据库

1、添加依赖库引用:

C#简单连接Oracle数据库_第1张图片

2、编写帮助类库

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

namespace DB_Helper4Oracle
{
     
    public class oracle_helper
    {
     
        public string ip = "";
        public ushort port = 1521;
        public string user = "";
        public string pass = "";
        public string service_name = "";
        /// 
        /// 出错信息
        /// 
        public string err = "0";

        /// 
        /// 读数据库
        /// 
        /// 传递SQL语句
        /// 返回DataTable,err = "0"成功;否则失败,原因查看:err
        public DataTable DB_Read(string sql_str)
        {
     
            DataTable dt = new DataTable();
            try
            {
     
                using (OracleConnection db_conn = new OracleConnection($@"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={ip})(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME={service_name})));User Id={user};Password={pass};"))
                {
     
                    db_conn.Open();
                    using (OracleCommand db_Command = new OracleCommand(sql_str, db_conn))
                    {
     
                        OracleDataAdapter db_Adapter = new OracleDataAdapter(db_Command);
                        db_Adapter.Fill(dt);
                        db_conn.Close();
                    }
                }
                err = "0";
            }
            catch (Exception ex)
            {
     
                dt = null;
                err = $@"读数据失败,原因:{ex.Message},SQL:{sql_str}";
            }
            return dt;
        }

        /// 
        /// 写数据库
        /// 
        /// 传递SQL语句
        /// true:成功;false:失败,原因查看:err
        public bool DB_Write(string sql_str)
        {
     
            try
            {
     
                using (OracleConnection db_conn = new OracleConnection($@"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={ip})(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME={service_name})));User Id={user};Password={pass};"))
                {
     
                    db_conn.Open();
                    using (OracleCommand db_Command = new OracleCommand(sql_str, db_conn))
                    {
     
                        db_Command.ExecuteNonQuery();
                    }
                    db_conn.Close();
                }
                err = "0";
                return true;
            }
            catch (Exception ex)
            {
     
                err = $@"写数据失败,原因:{ex.Message},SQL:{sql_str}";
                return false;
            }
        }

        public string GetLastErr()
        {
     
            return err;
        }

    }
}

3、调用测试

  private void Form1_Load(object sender, EventArgs e)
        {
     
            db = new oracle_helper()
            {
     
                ip = "*.*.*.*",
                port = 1521,
                user = "***",
                pass = "***",
                service_name = "***"
            };
            //敏感数据已做隐藏,根据实际项目填写即可
            string cph = "冀D***";
            DataTable dt = db.DB_Read($@"
SELECT * FROM V_CGJC WHERE CAR_NO='{cph}'
");
            MessageBox.Show(db.GetLastErr()); ;
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = dt;
        }

C#简单连接Oracle数据库_第2张图片

你可能感兴趣的:(C#,数据库,oracle,.net)