zoukankan      html  css  js  c++  java
  • 无需安装Oracle Client连接Oracle数据库

    介绍

    当我们采用 ODP.NET 检索Oracle 数据库的时候,Oracle客户端是必须安装。假如当时电脑上没有安装Oracle客户端,就不能这么用了,这时候Oracle.ManagedDataAccess.Client就派上用场了。


    背景知识

    在浏览本文之前, 我们必须了解 ADO.NET 框架, 以便通过其数据提供程序库连接到任何数据库。

    下面的示例代码是一个简单的控制台应用程序, 它使用 OracleManagedDataAccess 客户端库而不是 ODP.NET 连接到 oracle 数据库。在此应用程序中, 不需要在目标计算机上安装 oracle 客户端。

    在使用此代码之前, 您还需要具有正确的连接字符串, 以便访问数据库表。

    在这篇文章中, 我有一个连接到oracle数据库的有效连接串:

    User Id = test; Password = test; Data Source=localhost:1521; Pooling=false

    表的名字: tblTest

    因此, 如果有人要使用这个示例代码, 他们需要确保他们输入了上面提到的正确的细节。


    代码示例


    请按照以下步骤创建此示例控制台应用程序, 它在不使用 oracle 客户端的情况下连接 oracle 数据库。


    1. 创建C#控制台项目
    2. 右键单击项目并选择 "管理 nuget 包管理器"
    3. 搜索 oracle.ManagedDataAccess 库
    4. 在接受许可后, 安装动态库。
    5. 添加动态库引用后,参考下面的代码中指定的代码, (在顶部添加使用 Oracle.ManagedDataAccess.Client)
    6. 现在请参考下面的代码, 连接到不使用 oracle 客户端的 oracle 数据库

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Oracle.ManagedDataAccess.Client;
    
    namespace OracleManagedAccess
    {
        class Program
        {
            static void Main(string[] args)
            {
                OracleConnection con = new OracleConnection("User Id=test;Password=test; Data Source=localhost:1521; Pooling=false");
                OracleCommand cmd = new OracleCommand();
    
                Console.WriteLine("Welcome to OracleManagedAccess Connection! Please press 1 to connect to the Oracle database");
                int input = Convert.ToInt16(Console.ReadLine());
    
                if(input==1)
                {
                    con.Open();
                    cmd = con.CreateCommand();
                    cmd.CommandText = "Select * from tblTest";
    
                    OracleDataReader reader = cmd.ExecuteReader();
                    while(reader.Read())
                    {
                        Console.WriteLine(reader["Col1"].ToString()+ "    "+ reader["Col2"].ToString());
                    }
                }
                else
                {
                    Console.WriteLine("Please Try again");
                }
    
                Console.ReadKey();
            }
        }
    }
  • 相关阅读:
    Go语言中new()和 make()的区别详解
    对于Linux内核tty设备的一点理解
    中国移动MySQL数据库优化最佳实践
    深入分析Linux自旋锁
    JAVA大数据项目+整理的Mysql数据库32条军规
    MySQL DBA面试全揭秘
    LINUX 内核基础
    子查询
    linuxprobe----LINUX 基础课程目录学习
    从事分布式系统,计算,hadoop
  • 原文地址:https://www.cnblogs.com/lilunjia/p/7261719.html
Copyright © 2011-2022 走看看