C# 连接Oracle Demo

安装包 Oracle.ManagedDataAccess

测试时遇到的问题,  SERVICE_NAME=ORCL  这个不改,不然会报错TNS:监听程序当前无法识别连接描述符中请求的服务解决方案

然后 数据库测试  conn可以open 说明连同了

 

但是查询需要的SQL还是不行,提示表或视图不存在,但是表是存在的

检查后发现使用的账号 没有查询这个表的权限,这边需要DBA 协助了。。。。

 

using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConnectToOracle
{
    class Program
    {
        static void Main(string[] args)
        {
            string connString = "User Id=XXX;Password=XXX;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.10.10.10)(PORT=XXX)))(CONNECT_DATA=(SERVICE_NAME=XXX)))";
            OracleConnection conn = new OracleConnection(connString);
            try
            {
                conn.Open();
                Console.WriteLine(conn.State.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }
            finally
            {
                conn.Close();
            }
            Console.ReadKey();
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;
using CommonUtilsLibrary.Models;

namespace CommonUtilsLibrary.Utils
{
    public class OracleUtil:IDBUtil
    {
        public OracleUtil(OracleConPara para)
        {
            connStr = string.Format("User Id={0};Password={1};Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={2})(PORT={3})))(CONNECT_DATA=(SERVICE_NAME={4})))", para.UserID,para.Password,para.HostName,para.Port,para.ServiceName);
        }

        private string connStr { get; set; }

        #region 执行SQL语句,返回受影响行数
        public int ExecuteNonQuery(string sql, params OracleParameter[] parameters)
        {
            using (OracleConnection conn = new OracleConnection(connStr))
            {
                conn.Open();
                using (OracleCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        #endregion

        public int ExecuteNonQuery(string sql)
        {
            return ExecuteNonQuery(sql, new OracleParameter());
        }


        #region 执行SQL语句,返回DataTable;只用来执行查询结果比较少的情况
        public  DataTable ExecuteDataTable(string sql, params OracleParameter[] parameters)
        {
            using (OracleConnection conn = new OracleConnection(connStr))
            {
                conn.Open();
                using (OracleCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    OracleDataAdapter adapter = new OracleDataAdapter(cmd);
                    DataTable datatable = new DataTable();
                    adapter.Fill(datatable);
                    return datatable;
                }
            }
        }
        #endregion

        public  DataTable ExecuteDataTable(string sql)
        {
            return ExecuteDataTable(sql, new OracleParameter());
        }

        public int UpdateBatchCommand(List<string> commandStringList)
        {
            using (OracleConnection conn = new OracleConnection(connStr))
            {
                conn.Open();
                OracleTransaction m_OraTrans = conn.BeginTransaction();
                using (OracleCommand cmd = conn.CreateCommand())
                {
                    string tmpStr = "";
                    int influenceRowCount = 0;
                    try
                    {
                        foreach (string commandString in commandStringList)
                        {
                            tmpStr = commandString;
                            cmd.CommandText = tmpStr;
                            influenceRowCount += cmd.ExecuteNonQuery();
                        }
                        m_OraTrans.Commit();
                        return influenceRowCount;
                    }
                    catch (OracleException ex)
                    {
                        m_OraTrans.Rollback();
                        throw ex;
                    }
                }
                   
            }
        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值