C#处理读取使用US7ASCII的oracle数据库中文显示乱码问题

方式一:(推荐)Oracle Data Access Components (ODAC) +OleDbConnection

  • 该方式无需配置环境变量

1、 下载ODAC 组件,地址为https://www.oracle.com/technetwork/topics/dotnet/utilsoft-086879.htmlimage

2、 解压缩文件包,并安装组件,选择Oracle Client 即可

image
image

3、修改安装目录 C:\app\Administrator\product\11.2.0\client_1\Network\Admin(安装目录根据前面的安装可以有所不同) 下的 tnsnames.ora 文件。image

4、编写访问数据库的C#代码(OleDb驱动)。使用连接字符串1,可以省略步骤3的文件配置。

private static void Method1()
        {
            //连接字符串方式1:
            //string connString = "Provider=OraOLEDB.Oracle.1;Data Source=(DESCRIPTION =(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = IP地址)(PORT = 1521)))(CONNECT_DATA = (SID = orcl))) ;User Id=test;Password=123456";
            //连接字符串方式2:
            string connString = "Provider=OraOLEDB.Oracle.1;Data Source=orcl;User Id=test;Password=123456";
            using (OleDbConnection conn = new OleDbConnection(connString))
            {
                try
                {
                    conn.Open();
                    Console.WriteLine("连接成功!");
                    string sql = "select * from student";
                    OleDbCommand com = new OleDbCommand(sql, conn);
                    OleDbDataReader read = com.ExecuteReader();

                    while (read.Read())
                    {
                        String name = read["name"].ToString();
                        Console.WriteLine("name:{0}", name);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("连接失败!" + ex.Message);
                }
                finally
                {
                    if (conn != null)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
        }
posted @ 2022-09-20 21:14  his365  阅读(799)  评论(0编辑  收藏  举报