C # access Oracle12c through ODAC

Source: Internet
Author: User
Tags oracleconnection

Last night, Oracle12c was installed and the PLSQL Developer development environment was ready. Let's continue to improve today and talk about how to access Oracle12c in C.

In fact, we can also use the early ADO. NET connection, but it is not recommended in. NET Framework4.0. If we still reference System. Data. OracleClient, when using OracleConnection, OracleCommand, and other Database Core operation classes in the program code, a green wave will prompt you that the class is outdated.

650) this. width = 650; "src =" http://img.blog.csdn.net/20140116235436421? Watermark/2/text/character =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/Center "style =" border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; "alt =" Center "/>

Place the mouse on the green wavy line and the following prompt will be displayed:

650) this. width = 650; "src =" http://img.blog.csdn.net/20140116235608671? Watermark/2/text/character =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/Center "style =" border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; "alt =" Center "/>

It indicates that the System. Data. OracleClient. OracleConnection class is outdated and a Microsoft official link is provided later.

We open the browser output above URL: http://go.microsoft.com/fwlink? LinkID = 144260. If you cannot use English, use this address: http://msdn.microsoft.com/zh-cn/library/77d8yct7.aspx.

The description in the URL is very intuitive:

650) this. width = 650; "src =" http://img.blog.csdn.net/20140117000034468? Watermark/2/text/character =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/Center "style =" border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; "alt =" Center "/>

It should be noted that, although the prompt is outdated, the program still runs. However, Microsoft officially recommends a more efficient third-party Oracle provider.

The following describes the ODACOracle Data Access Components component provided by Oracle that is more efficient than ADO. NET.

This component can also be downloaded from the official website. Http://www.oracle.com/technetwork/database/windows/downloads/index-090165.html

Select an appropriate version based on the Oracle version we installed earlier. Because I previously installed Oracle 12c R1 with 64-bit, we chose to download the file as shown in.

650) this. width = 650; "src =" http://img.blog.csdn.net/20140117000951937? Watermark/2/text/character =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/Center "style =" border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; "alt =" Center "/>

After downloading the file, decompress it to the folder and run the setup.exe file for installation. Our next version supports VS2010 and VS2012. Since I installed VS2012, You need to manually install it to VS2012 during the installation process, and the other is the next step. The installation time is not long. Please wait until the installation is complete and then exit.

When the environment is ready, we can test whether it can be connected to Oracle 12c in VS2012.

The test process is simple:

Open VS2012, select view in the menu bar, and then select server resource manager. Right-click the data connection and select Add connection from the pop-up menu. The following page is displayed. After entering the account, password, and other elements, click the test connection button to get the connection feedback success or failure ).

650) this. width = 650; "src =" http://img.blog.csdn.net/20140117001724390? Watermark/2/text/character =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/Center "style =" border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; "alt =" Center "/>

If the test connection is successful ." Indicates that we have successfully connected to Oracle 12c through the ODAC component provided by Oracle.

650) this. width = 650; "src =" http://img.blog.csdn.net/20140117001933562? Watermark/2/text/character =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/Center "style =" border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; "alt =" Center "/>

Then we can write code in VS to test it. We still use the test case above to output the system time of the current database.

First, we need to reference Oracle. DataAccess in the project, and then we can imitate ADO. NET code to write ODP. NET code.

String connString = "Data Source = orcl; User ID = C # guwei4037; Password = 123456; DBA Privilege = SYSDBA;"; using (Oracle. dataAccess. client. oracleConnection conn = new Oracle. dataAccess. client. oracleConnection (connString) {if (conn. state! = ConnectionState. open) conn. open (); // Open the database to connect to Oracle. dataAccess. client. oracleCommand cmd = new Oracle. dataAccess. client. oracleCommand ("select sysdate from dual", conn); // execute an SQL statement object obj = cmd. executeScalar (); // return the result Console in the first column of the first row. writeLine (obj! = Null? Obj. ToString (): "null ");}

Run the console application.


650) this. width = 650; "src =" http://img.blog.csdn.net/20140117002449125? Watermark/2/text/character =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/Center "style =" border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; "alt =" Center "/>

OK. The database system time is output successfully. ODAC is successfully used to access Oracle12c.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.