c#程序,oracle使用Devart驱动解决第第三方库是us7ascii,数据乱码的问题

最近做项目,要跟对方系统的库进行读写,结果发现对方采用的是oracle的us7ascii编码,我们系统默认采用的是ZHS16GBK,导致我们客户端读取和写入对方库的数据都是乱码,搜索网上,发现需要采用独立的oracle驱动去处理,最后采用Devart驱动,可以指定字符集编码

添加引用2个DLL:

数据库操作代码如下:(查询)

cs 复制代码
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

using Devart.Data.Oracle;

namespace WindowsOrcleDevart
{
    /// <summary>
    /// 使用OracleDevart驱动
    /// </summary>
    public class ProviderDevart
    {
        private OracleConnection dbConnection;
        private OracleTransaction dbTransaction;
        private string _strConn = "";
        private string path = AppDomain.CurrentDomain.BaseDirectory + "logs\\调用数据库";
        public string Err = "";
        public string StrConn { get => _strConn; set => _strConn = value; }
        public int BeginTransaction()
        {
            //this.Err = "";
            int result = -1;
            try
            {
                GetSingerConnection();
                this.dbTransaction = this.dbConnection.BeginTransaction();
                result = 1;
            }
            catch (Exception message)
            {
                this.dbTransaction.Dispose();
                this.dbTransaction = null;
                Err = "开始事务异常," + message.Message;
                WriteLog(path, Err);
            }
            return result;
        }
        // 定义一个静态变量来保存类的实例
        private static ProviderDevart s_instance;
        /// <summary>
        /// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点
        /// </summary>
        /// <returns></returns>
        public static ProviderDevart Instance
        { 
            // 当第一个线程运行到这里时,此时会对locker对象 "加锁",
            // 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁
            // lock语句运行完之后(即线程运行完之后)会对该对象"解锁"
            // 双重锁定只需要一句判断就可以了
            get
            {
                if (s_instance == null)
                {
                    lock (locker)
                    {
                        if (s_instance == null)
                        {
                            s_instance = new ProviderDevart();
                        }
                    }
                }
                return s_instance;
            }
        }

        #region 单例锁  定义一个标识确保线程同步
        private static readonly object locker = new object();
        #endregion

        private void GetSingerConnection()
        {
            if (this.dbConnection != null)
            {
                if (dbConnection.State != ConnectionState.Open)
                {
                    dbConnection.Open();
                }
            }
            else
            {
                this.dbConnection = new OracleConnection(_strConn);
                this.dbConnection.Open();
            }
        }

        public int TransactionCommit()
        {
            //this.Err = "";
            int result = -1;
            try
            {
                GetSingerConnection();
                if (this.dbTransaction != null)
                {
                    this.dbTransaction.Commit();
                    this.dbTransaction.Dispose();
                    this.dbTransaction = null;
                }
                result = 1;
            }
            catch (Exception message)
            {
                if (this.dbTransaction != null)
                {
                    this.dbTransaction.Dispose();
                    this.dbTransaction = null;
                }
                Err = "提交事务异常," + message.Message;
                WriteLog(path, Err);
            }
            return result;
        }

        public int TransactionRollBack()
        {
            //this.Err = "";
            int result = -1;
            try
            {
                GetSingerConnection();
                if (this.dbTransaction != null)
                {
                    this.dbTransaction.Rollback();
                    this.dbTransaction.Dispose();
                    this.dbTransaction = null;
                }
                result = 1;
            }
            catch (Exception message)
            {
                if (this.dbTransaction != null)
                {
                    this.dbTransaction.Dispose();
                    this.dbTransaction = null;
                }
                Err = "回滚事务异常," + message.Message;
                WriteLog(path, Err);
            }
            return result;
        }

        /// <summary>
        /// 插入/更新/删除表数据
        /// </summary>
        /// <param name="strSql"></param>
        /// <returns>返回SQL执行的影响行数,返回-1表示出现了内部错误</returns>
        public int ExecuteNonQuery(string strSql)
        {
            //this.Err = "";
            OracleGlobalization applicationInfo = OracleGlobalization.GetApplicationInfo();
            applicationInfo.ClientCharacterSet = "us7ascii";
            OracleGlobalization.SetApplicationInfo(applicationInfo);

            int result = -1;
            try
            {
                GetSingerConnection();
                WriteLog(path, " 开始");
                WriteLog(path, "strConn:" + _strConn);
                WriteLog(path, "strSql:" + strSql);
                if (_strConn == "" || strSql == "")
                {
                    Err = "strConn 或 strSql 为空,无法执行!";
                }
                else
                {
                    OracleCommand cmd = dbConnection.CreateCommand();
                    if (this.dbTransaction != null)
                    {
                        cmd.Transaction = this.dbTransaction;
                    }
                    cmd.CommandText = strSql;
                    result = cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Err = "插入更新数据异常," + ex.Message;
                WriteLog(path, Err);
            }

            WriteLog(path, " 结束");
            return result;
        }

        /// <summary>
        /// 查询表数据
        /// </summary>
        /// <param name="strSql">SQL语句</param>
        /// <param name="data">返回数据为DataTable</param>
        /// <returns></returns>
        public int FillDataTable(string strSql, out DataTable data)
        {
            //this.Err = "";
            data = new DataTable();
            OracleGlobalization applicationInfo = OracleGlobalization.GetApplicationInfo();
            applicationInfo.ClientCharacterSet = "us7ascii";
            OracleGlobalization.SetApplicationInfo(applicationInfo);

            int result = -1;
            OracleCommand oleDbCommand = null;
            try
            {
                GetSingerConnection();
                WriteLog(path, " 开始");
                WriteLog(path, "strConn:" + _strConn);
                WriteLog(path, "strSql:" + strSql);
                oleDbCommand = new OracleCommand(strSql, dbConnection);
                oleDbCommand.CommandType = CommandType.Text;
                using (OracleDataAdapter oleDbDataAdapter = new OracleDataAdapter(oleDbCommand))
                {
                    DataSet ds = new DataSet();
                    oleDbDataAdapter.Fill(ds, "ReturnTable");
                    if (ds != null && ds.Tables.Count > 0)
                    {
                        data = ds.Tables[0];
                    }
                }
                result = 1;
            }
            catch (Exception ex)
            {
                Err = "查询数据异常," + ex.Message;
                WriteLog(path, Err);
            }
            WriteLog(path, " 结束");
            return result;
        }

        private bool dbClose(OracleConnection conn, OracleCommand cmd, OracleTransaction tran)
        {
            bool result;
            if (cmd != null)
            {
                cmd.Dispose();
                GC.Collect();
                result = false;
            }
            else if (conn != null)
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
                conn.Dispose();
                GC.Collect();
                result = true;
            }
            else
            {
                GC.Collect();
                result = false;
            }
            return result;
        }

        public void WriteLog(string filePath, string message)
        {
            try
            {
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                string fileName = filePath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
                string msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:") + DateTime.Now.Millisecond + "  " + message;
                System.IO.StreamWriter sw = System.IO.File.AppendText(fileName);
                sw.WriteLine(msg);
                sw.Close();
            }
            catch
            {
            }
        }
    }
}

客户端访问示例:

cs 复制代码
private void button1_Click(object sender, EventArgs e)
{
    ProviderDevart providerDevart = ProviderDevart.Instance;
    providerDevart.StrConn = "data source=XXX.XX.8.X:1521/orcl;user id=#####;password=######;";
    providerDevart.FillDataTable("SELECT '测试数据' as T1 FROM DUAL  ",out DataTable dt);
    MessageBox.Show(dt.Rows[0]["T1"].ToString());
}

源码,压缩包见地址:

c#程序,oracle使用Devart驱动解决第第三方库是us7ascii,数据乱码的问题资源-CSDN文库

相关推荐
superman超哥11 分钟前
04 深入 Oracle 并发世界:MVCC、锁、闩锁、事务隔离与并发性能优化的探索
数据库·oracle·性能优化·dba
engchina1 小时前
Neo4j 和 Python 初学者指南:如何使用可选关系匹配优化 Cypher 查询
数据库·python·neo4j
engchina1 小时前
使用 Cypher 查询语言在 Neo4j 中查找最短路径
数据库·neo4j
尘浮生1 小时前
Java项目实战II基于Spring Boot的光影视频平台(开发文档+数据库+源码)
java·开发语言·数据库·spring boot·后端·maven·intellij-idea
威哥爱编程1 小时前
SQL Server 数据太多如何优化
数据库·sql·sqlserver
小华同学ai1 小时前
AJ-Report:一款开源且非常强大的数据可视化大屏和报表工具
数据库·信息可视化·开源
Acrelhuang2 小时前
安科瑞5G基站直流叠光监控系统-安科瑞黄安南
大数据·数据库·数据仓库·物联网
Mephisto.java2 小时前
【大数据学习 | kafka高级部分】kafka的kraft集群
大数据·sql·oracle·kafka·json·hbase
Mephisto.java2 小时前
【大数据学习 | kafka高级部分】kafka的文件存储原理
大数据·sql·oracle·kafka·json
十叶知秋2 小时前
【jmeter】jmeter的线程组功能的详细介绍
数据库·jmeter·性能测试