. NET using ODP. NET to connect to Oracle Database notes, odp. netoracle

Source: Internet
Author: User
Tags oracleconnection

. NET using ODP. NET to connect to Oracle Database notes, odp. netoracle

1. Development side install ODP. NET.: http://www.oracle.com/technetwork/topics/dotnet/downloads/index.html

Note: We recommend that you download the same odp version as the server version. Otherwise, there will be many strange problems. as for whether to use 32-bit or 64-bit, I have tried no difference (server 64-bit, development machine 32-bit ).

2. vs open a console program and reference Oracle. DataAccess. dll. You can reference it from gac or local paths. The test code is as follows:

Using System; using System. collections. generic; using System. text; using Oracle. dataAccess. client; // The odp class must be using Oracle. dataAccess. types; namespace ConsoleApplication2 {class Program {static void Main (string [] args) {string conString = "Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = 127.0.0.1) (PORT = 1521) "+" (CONNECT_DATA = (SID = testdb); User Id = dba; Password = dbb ;"; // write the connection string. The above two methods can be used for connection or put on the Web. co In nfig. OracleConnection con = new OracleConnection (conString); OracleCommand command = new OracleCommand (); command. connection = con; command. commandText = "select * from T_users"; // change it to your own table con. open (); OracleDataReader dr = command. executeReader (); // C # while (dr. read () {Console. writeLine (dr [0]. toString ();} // Console. writeLine (returnResult); Console. read ();}}}

If the data can be accessed normally, it proves OK.

Note:

1. if an error is reported

Unprocessed exception: System. IO. FileNotFoundException: failed to Load file or assembly "Oracle. Dat

AAccess, Version = 2.112.1.2, Culture = neutral, PublicKeyToken = 89b483f429c47342 "or
One of its dependencies. The system cannot find the specified file.

Solution: The reference path of gac or Oracle. DataAccess is abnormal. We recommend that you copy the file to the released bin directory.

2. If an error is reported

Unprocessed exception: System. TypeInitializationException: "Oracle. DataAccess. Client. Or
AcleConnection"Type. ---> Oracle. DataAccess. Client. CMDL
EException The provider is not compatible with the version of Oracle client
In Oracle. DataAccess. Client. OracleInit. Initialize ()
In Oracle. DataAccess. Client. OracleConnection.. cctor ()
--- End of the internal exception stack trace ---
In Oracle. DataAccess. Client. OracleConnection.. ctor (String connectionString)
In leleapplication2.program. Main (String [] args)

Solution: This indicates that the dll environment on which oracle depends is loaded incorrectly. The odp version installed on the development end may be different from that on the server. or the client does not have a dynamic database running in oracle. it is recommended that the development end reinstall the consistent version or re-Execute oracle. dataaccess gac configuration.

3. Client

If the client does not want to install odp.net, the following files must be decompressed in the odp installation path of the development end that successfully connects to the server and placed in the directory where the published application program runs.

I have tested whether oracle. dataaccess. dll is registered with the gac of the client.

File List:

Oci. dll

Ociw32.dll

Orannzsbb11.dll

Oraocci11.dll

Oraociei11.dll

Bin \ OraOps11w. dll

Odp.net \ x \ Oracle. DataAccess. dll (x indicates omitted path)

 

Enjoy!


Which method is better to connect to the oracle database?

Don't worry about using it. The so-called small problems are because your connection is not well established, and the underlying data access program is not written stably. If the data connection is unstable, Microsoft can die. Haha

Net connection to the Oracle database

C # connecting to the Oracle database (querying data)

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
// You must add system. data. oracleclient to both this line and the next line in reference.
Using System. Data. OracleClient;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;

Namespace WindowsApplication1
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Private void button#click (object sender, EventArgs e)
{
# Region is manually written from region to endregion. Others are automatically generated by the system.
// Define the database connection string
String constring = "data source = wzd; user = wzd; password = wzd ;";
// Connect
OracleConnection conn = new OracleConnection (constring );
Try
{
Conn. Open (); // Open the specified connection
OracleCommand com = conn. CreateCommand ();
// Write the SQL statement that you want to execute
Com. CommandText =
"Select name from mytable where card_no = '000000 '";
OracleDataReader odr = com. ExecuteReader ();
// Read data. If the returned value is false, it indicates the end of the record set.
While (odr. Read ())
{
// Display the read value to the defined control.
This. lbl. Text = odr. GetOracleString (0). ToString ();
}
Odr. Close (); // Close reader. This must be written.
}
Catch
{
Messa ...... remaining full text>

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.