通过 C# 连接到 Oracle 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12568100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:42:42  来源:igfitidea点击:

Connecting to Oracle Database through C#?

c#oraclec#-4.0

提问by RG-3

I need to connect to a Oracle DB (external) through Visual Studio 2010. But I dont want to install Oracle on my machine. In my project I referenced: System.Data.OracleClient. But its not fulfilling the need. I have an "Oracle SQL Developer IDE"in which I run SQL queries against oracle db.

我需要通过 Visual Studio 2010 连接到 Oracle DB(外部)。但我不想在我的机器上安装 Oracle。在我的项目中,我引用了:System.Data.OracleClient。但它不能满足需要。我有一个“Oracle SQL Developer IDE”,我在其中针对 oracle db 运行 SQL 查询。

I have this code so far:

到目前为止我有这个代码:

 private static string GetConnectionString()
    {
        String connString = "host= serverName;database=myDatabase;uid=userName;pwd=passWord";
        return connString;
    }

 private static void ConnectingToOracle()
    {
        string connectionString = GetConnectionString();
        using (OracleConnection connection = new OracleConnection())
        {
            connection.ConnectionString = connectionString;
            connection.Open();
            Console.WriteLine("State: {0}", connection.State);
            Console.WriteLine("ConnectionString: {0}",
                              connection.ConnectionString);

            OracleCommand command = connection.CreateCommand();
            string sql = "SELECT * FROM myTableName";
            command.CommandText = sql;

            OracleDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                string myField = (string)reader["MYFIELD"];
                Console.WriteLine(myField);
            }
        }
    }

So far I read these blogs:

到目前为止,我阅读了这些博客:

http://st-curriculum.oracle.com/tutorial/DBXETutorial/index.htm

http://st-curriculum.oracle.com/tutorial/DBXETutorial/index.htm

http://blogs.msdn.com/b/kaevans/archive/2009/07/18/connecting-to-oracle-from-visual-studio.aspx

http://blogs.msdn.com/b/kaevans/archive/2009/07/18/connecting-to-oracle-from-visual-studio.aspx

So far I have not downloaded anything from Oracle. What steps should I take to make this happen?

到目前为止,我还没有从 Oracle 下载任何东西。我应该采取哪些步骤来实现这一目标?

采纳答案by Prabhu Murthy

First off you need to download and install ODP from this site http://www.oracle.com/technetwork/topics/dotnet/index-085163.html

首先,您需要从该站点下载并安装 ODP http://www.oracle.com/technetwork/topics/dotnet/index-085163.html

After installation add a reference of the assembly Oracle.DataAccess.dll.

安装后添加程序集Oracle.DataAccess.dll的引用。

Your are good to go after this.

你很高兴去追求这个。

using System; 
using Oracle.DataAccess.Client; 

class OraTest
{ 
    OracleConnection con; 
    void Connect() 
    { 
        con = new OracleConnection(); 
        con.ConnectionString = "User Id=<username>;Password=<password>;Data Source=<datasource>"; 
        con.Open(); 
        Console.WriteLine("Connected to Oracle" + con.ServerVersion); 
    }

    void Close() 
    {
        con.Close(); 
        con.Dispose(); 
    } 

    static void Main() 
    { 
        OraTest ot= new OraTest(); 
        ot.Connect(); 
        ot.Close(); 
    } 
}

回答by Elsadig Adam

The next approach work to me with Visual Studio 2013 Update 4 1- From Solution Explorer right click on References then select add references 2- Assemblies > Framework > System.Data.OracleClient > OK and after that you free to add using System.Data.OracleClient in your application and deal with database like you do with Sql Server database except changing the prefix from Sql to Oracle as in SqlCommand become OracleCommand for example to link to Oracle XE

下一个方法适用于 Visual Studio 2013 Update 4 1- 从解决方案资源管理器右键单击引用,然后选择添加引用 2- 程序集 > 框架 > System.Data.OracleClient > OK,然后您可以使用 System.Data 自由添加。您的应用程序中的 OracleClient 并像处理 Sql Server 数据库一样处理数据库,除了将前缀从 Sql 更改为 Oracle,如 SqlCommand 变为 OracleCommand 例如链接到 Oracle XE

OracleConnection oraConnection = new OracleConnection(@"Data Source=XE; User ID=system; Password=*myPass*");
public void Open()
{
if (oraConnection.State != ConnectionState.Open)
{
oraConnection.Open();
}
}
public void Close()
{
if (oraConnection.State == ConnectionState.Open)
{
oraConnection.Close();
}}

and to execute some command like INSERT, UPDATE, or DELETE using stored procedure we can use the following method

并使用存储过程执行一些命令,如 INSERT、UPDATE 或 DELETE,我们可以使用以下方法

public void ExecuteCMD(string storedProcedure, OracleParameter[] param)
{
OracleCommand oraCmd = new OracleCommand();
oraCmd,CommandType = CommandType.StoredProcedure;
oraCmd.CommandText = storedProcedure;
oraCmd.Connection = oraConnection;

if(param!=null)
{
oraCmd.Parameters.AddRange(param);
}
try
{
oraCmd.ExecuteNoneQuery();
}
catch (Exception)
{
MessageBox.Show("Sorry We've got Unknown Error","Connection Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}

回答by techExplorer

Basically in this case, System.Data.OracleClientneed access to some of the oracle dll which are not part of .Net. Solutions:

基本上在这种情况下,System.Data.OracleClient需要访问一些不属于 .Net 的 oracle dll。解决方案:

  • Install Oracle Client , and add bin location to Path environment varaible of windows OR
  • Copy oraociicus10.dll (Basic-Lite version) or aociei10.dll (Basic version), oci.dll, orannzsbb10.dll and oraocci10.dll from oracle client installable folder to bin folder of application so that application is able to find required dll
  • 安装 Oracle Client ,并将 bin 位置添加到 windows 的 Path 环境变量或
  • 将oraociicus10.dll(Basic-Lite版)或aociei10.dll(Basic版)、oci.dll、orannzsbb10.dll和oraocci10.dll从oracle客户端安装文件夹复制到应用程序的bin文件夹中,以便应用程序能够找到所需的dll

回答by honzakuzel1989

You can use Oracle.ManagedDataAccessNuGet package too (.NET >= 4.0, database >= 10g Release 2).

您也可以使用Oracle.ManagedDataAccessNuGet 包(.NET >= 4.0,数据库 >= 10g 第 2 版)。

回答by Boern

Using Nuget

使用 Nuget

  1. Right click Project, select Manage NuGet packages...
  2. Select the Browsetab, search for Oracleand install Oracle.ManagedDataAccess
  1. 右键单击项目,选择 Manage NuGet packages...
  2. 选择Browse选项卡,搜索Oracle并安装Oracle.ManagedDataAccess

Oracle NuGet package

Oracle NuGet 包

  1. In code use the following command (Ctrl+.to automatically add the using directive).

  2. Note the different DataSource string which in comparison to Java is different.

    // create connection
    OracleConnection con = new OracleConnection();
    
    // create connection string using builder
    OracleConnectionStringBuilder ocsb = new OracleConnectionStringBuilder();
    ocsb.Password = "autumn117";
    ocsb.UserID = "john";
    ocsb.DataSource = "database.url:port/databasename";
    
    // connect
    con.ConnectionString = ocsb.ConnectionString;
    con.Open();
    Console.WriteLine("Connection established (" + con.ServerVersion + ")");
    
  1. 在代码中使用以下命令(Ctrl+.自动添加 using 指令)。

  2. 请注意不同的 DataSource 字符串与 Java 相比有所不同。

    // create connection
    OracleConnection con = new OracleConnection();
    
    // create connection string using builder
    OracleConnectionStringBuilder ocsb = new OracleConnectionStringBuilder();
    ocsb.Password = "autumn117";
    ocsb.UserID = "john";
    ocsb.DataSource = "database.url:port/databasename";
    
    // connect
    con.ConnectionString = ocsb.ConnectionString;
    con.Open();
    Console.WriteLine("Connection established (" + con.ServerVersion + ")");