简体   繁体   English

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

[英]Connecting to Oracle Database through C#?

I need to connect to a Oracle DB (external) through Visual Studio 2010. But I dont want to install Oracle on my machine. 我需要通过Visual Studio 2010连接到Oracle DB(外部)。但我不想在我的机器上安装Oracle。 In my project I referenced: System.Data.OracleClient . 在我的项目中,我引用了: 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. 我有一个“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. 到目前为止,我还没有从Oracle下载任何东西。 What steps should I take to make this happen? 我应该采取什么措施来实现这一目标?

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(); 
    } 
}

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

Using Nuget 使用Nuget

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

Oracle NuGet包

  1. In code use the following command ( Ctrl + . to automatically add the using directive). 在代码中使用以下命令( Ctrl + 自动添加using指令)。

  2. Note the different DataSource string which in comparison to Java is different. 请注意,与Java相比,不同的DataSource字符串是不同的。

     // 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 + ")"); 

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- Assemblies> Framework> System.Data.OracleClient>确定,之后可以使用System.Data自由添加。您的应用程序中的OracleClient和您使用Sql Server数据库一样处理数据库,除了在SqlCommand中将前缀从Sql更改为Oracle成为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);
}
}

Basically in this case, System.Data.OracleClient need access to some of the oracle dll which are not part of .Net. 基本上在这种情况下, System.Data.OracleClient需要访问一些不属于.Net的oracle dll。 Solutions: 解决方案:

  • Install Oracle Client , and add bin location to Path environment varaible of windows OR 安装Oracle客户端,并将bin位置添加到Windows OR的Path环境中
  • 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 将oraociicus10.dll(Basic-Lite版本)或aociei10.dll(基本版),oci.dll,orannzsbb10.dll和oraocci10.dll从oracle客户端可安装文件夹复制到应用程序的bin文件夹,以便应用程序能够找到所需的dll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM