简体   繁体   中英

How to connect Oracle Database to Visual Studio C# project

I am trying to connect database created in Oracle 11g. This is simple code I wanted to use to check connection in VS, but It doesn't even works:

    OleDbConnection conn = new OleDbConnection("Provider=MSDAORA; Data Source = localhost; User ID = library; Password = library; Unicode = True");
    private void button1_Click(object sender, EventArgs e)
    {
        conn.Open();
    }

I get this error when I click button:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Error while trying to retrieve text for error ORA-01019

When I go to server explorer --> add connection and type in my database information, it works fine and connection shows up in server explorer.

I also get a warning in VS:

Severity Code Description Project File Line Suppression State Warning There was a mismatch between the processor architecture of the project being built "x86" and the processor architecture of the reference "Oracle.DataAccess, Version=4.112.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=AMD64", "AMD64". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project. Biblioteka1

I'd really recommend you use Oracle's ODP.net. It works very efficiently between Oracle and .NET and can take advantage of many of the low level features (such as bulk inserts/updates) available through the Oracle Call Interface (OCI).

From there, ODP.net has a OracleConnectionStringBuilder class that demistifys the connection string difficulties you have with most databases:

OracleConnectionStringBuilder sb = new OracleConnectionStringBuilder();
sb.DataSource = "<your datasource>";
sb.UserID = "library";
sb.Password = "library";

OracleConnection conn = new OracleConnection(sb.ToString());
conn.Open();

If your database is remote, Oracle's EZ Connect makes it nice to not have to worry about TNS names:

sb.DataSource = "hostname.whatever.com:1521/ServiceName";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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