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文库

相关推荐
梦想平凡19 分钟前
PHP 微信棋牌开发全解析:高级教程
android·数据库·oracle
TianyaOAO29 分钟前
mysql的事务控制和数据库的备份和恢复
数据库·mysql
Ewen Seong41 分钟前
mysql系列5—Innodb的缓存
数据库·mysql·缓存
码农老起1 小时前
企业如何通过TDSQL实现高效数据库迁移与性能优化
数据库·性能优化
向宇it1 小时前
【从零开始入门unity游戏开发之——C#篇24】C#面向对象继承——万物之父(object)、装箱和拆箱、sealed 密封类
java·开发语言·unity·c#·游戏引擎
夏木~2 小时前
Oracle 中什么情况下 可以使用 EXISTS 替代 IN 提高查询效率
数据库·oracle
W21552 小时前
Liunx下MySQL:表的约束
数据库·mysql
黄名富2 小时前
Redis 附加功能(二)— 自动过期、流水线与事务及Lua脚本
java·数据库·redis·lua
言、雲2 小时前
从tryLock()源码来出发,解析Redisson的重试机制和看门狗机制
java·开发语言·数据库
一个程序员_zhangzhen3 小时前
sqlserver新建用户并分配对视图的只读权限
数据库·sqlserver