Connecting to Oracle Database through C#?

后端 未结 5 1889
感动是毒
感动是毒 2020-11-28 05:41

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.Ora

相关标签:
5条回答
  • 2020-11-28 06:13

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

    • 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
    0 讨论(0)
  • 2020-11-28 06:25

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

    After installation add a reference of the assembly 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(); 
        } 
    }
    
    0 讨论(0)
  • 2020-11-28 06:26

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

    0 讨论(0)
  • 2020-11-28 06:29

    Using Nuget

    1. Right click Project, select Manage NuGet packages...
    2. Select the Browse tab, search for Oracle and install Oracle.ManagedDataAccess

    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 + ")");
      
    0 讨论(0)
  • 2020-11-28 06:38

    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

    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

    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);
    }
    }
    
    0 讨论(0)
提交回复
热议问题