How C # uses OLE DB to connect local or remote Oracel databases

Source: Internet
Author: User
Tags ole

For the development of Oracle database in C #, it is convenient to use the Odp.net method provided by Oracle, which is also better in terms of performance and compatibility.

However, for non-intended use, how to connect using OLE DB

The way to connect everyone can be the capital of the more understanding is ADO, but the most important thing is how the connection string.

Paste the test code first:

using (OleDbConnection ole = new OleDbConnection (TEP))
{

OleDbCommand cmd = new OleDbCommand ("SELECT * from FirstClass");
Cmd. Connection = OLE;
OleDbDataAdapter dr = new OleDbDataAdapter (cmd);
DataSet ds = new DataSet ();
Dr. Fill (DS);
Datagridview1.datasource = ds. Tables[0];

}

You can see that it is very simple, but the above tep is the connection string to write it.

There are several ways to configure and write with DSN, but it can be time-consuming for people who don't understand the ODBC configuration very well.

How do I use the connection based on odp.net and then get the connection string based on the debug hint of vs? Relatively simple

The way I get it is as follows:

1. Odp.net's connection Name

String connstring = @ "Data source= (description= (address= (protocol=tcp) (host=127.0.0.1) (port=1521))
(Connect_data= (SID=ORCL))); User Id=appserveruser; password=1234; ";

The above is the ODP-mode connection string

2. We use this to debug by default

This is a hint of VS

That is, we did not provide provider simple we add the sample first try

3. Add the test

This is the added connection string tep = "Data source= (DESCRIPTION = (Address_list = (ADDRESS = (PROTOCOL = TCP) (HOST = 127.0.0.1) (PORT = 1 521))) (Connect_data = (service_name = ORCL))); User Id=appserveruser; password=1234; Provider=sqloledb; ";

4. Tips for VS debugging

What's weird is that we're using Oracle, but the hint is SQL Server why?

5. The reason is that the provider we use is not Oracle's

6. How to Solve

Using Oracle's provider name

Should be the following:

Provider=oraoledb.oracle;

7. Final connection string

String tep = "Data source= (DESCRIPTION = (Address_list = (ADDRESS = (PROTOCOL = TCP) (HOST = 127.0.0.1) (PORT = 1521)) (Connec T_data = (service_name = ORCL)); User Id=appserveruser; password=1234; provider=oraoledb.oracle; ";

8. Using VS for debugging

OK data gets successful

This approach, while complex, can be used to obtain remote and local Oracle databases

If you notice, the connection string here is based on the format of the Tnsnames.ora file, in fact, the configuration of this file is to connect Oracle data

The most important configuration of the library. Hope to be of help to everyone.

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.