C # connects Oracle in many ways.

Source: Internet
Author: User
Tags odbc ole oracleconnection

Not much nonsense to say directly to the point:

First we built a user in Oracle database called Lisi, the password is Lisi, under this user to create a table called "Users", the table under the new three data.

Method One: Use OLE DB to connect to Oracle.

1    #regionUsing OLE DB mode2Console.WriteLine ("OLE DB mode");3OleDbConnection OleDbConnection =NewOleDbConnection ("provider=oraoledb.oracle; Data source=mldn; User Id=lisi; Password=lisi;");4OleDbCommand OleDbCommand =NewOleDbCommand ();5Oledbcommand.commandtext ="SELECT * from USERS";6Oledbcommand.commandtype =CommandType.Text;7Oledbcommand.connection =OleDbConnection;8 Oledbconnection.open ();9             varOledbreader =OleDbCommand.ExecuteReader ();Ten              while(Oledbreader.read ()) One             { AConsole.WriteLine ("Name:"+oledbreader[1]); -             } - oledbconnection.close (); the   #endregion

Way two: Using Oracle.ManagedDataAccess.Client method, before using, we need to find oracle.manageddataaccess in NuGet, then install, one of the advantages of this way is not to install the Oracle client on the client, the version is Developed by Oracle, and Microsoft has given up its own System.Data.OracleClient approach (described below), it is recommended to use this approach.

The code is as follows:

  #regionOracleConnection//oracleconnection connection = new OracleConnection ("Password=lisi; User id=lisi;data source= (description= (address_list= (address=) (PROTOCOL=TCP) (host=192.168.229.138)) ( Connect_data= (server=dedicated) (service_name=****))); You can use this method if you do not have a client installedConsole.WriteLine ("Oracle.ManagedDataAccess.Client Way"); OracleConnection oralceconnection=NewOracleConnection ("Data source=mldn; User Id=lisi; Password=lisi;");            Oralceconnection.open (); OracleCommand OracleCommand=NewOracleCommand (); Oraclecommand.commandtext="SELECT * from USERS"; Oraclecommand.commandtype=CommandType.Text; Oraclecommand.connection=oralceconnection; varOraclereader =OracleCommand.ExecuteReader ();  while(Oraclereader.read ()) {Console.WriteLine ("Name:"+ oraclereader[1]);            } oralceconnection.close (); #endregion

Method Three: Using ODBC mode

        #regionOdbcConsole.WriteLine ("ODBC mode"); OdbcConnection OdbcConnection=NewOdbcConnection ("driver={microsoft ODBC for Oracle}; SERVER=MLDN; Uid=lisi; Pwd=lisi;");         Odbcconnection.open (); OdbcCommand OdbcCommand=NewOdbcCommand (); Odbccommand.commandtext="SELECT * from USERS"; Odbccommand.commandtype=CommandType.Text; Odbccommand.connection=OdbcConnection; varOdbcreader=Odbccommand.executereader ();  while(Odbcreader.read ()) {Console.WriteLine ("Name:"+ odbcreader[1]); } odbcconnection.close ();#endregion

Mode four: The use of the System.Data.OracleClient method (the way is outdated, not recommended).

Although it is obsolete, it can still be used, the code is as follows:

1       #region2 3Console.WriteLine ("System.Data.OracleClient Way");4System.Data.OracleClient.OracleConnection clientconnection =NewSystem.Data.OracleClient.OracleConnection ();5Clientconnection.connectionstring ="Data source=mldn; User Id=lisi; Password=lisi;";6System.Data.OracleClient.OracleCommand Clientcommand =NewSystem.Data.OracleClient.OracleCommand ();7 8Clientcommand.commandtext ="SELECT * from USERS";9Clientcommand.connection =clientconnection;TenClientcommand.commandtype =CommandType.Text; One Clientconnection.open (); A          varClientreader =Clientcommand.executereader (); -           while(Clientreader.read ()) -          { the  -Console.WriteLine ("Name:"+ clientreader[1]); -          } - clientconnection.close (); +             #endregion

Mode five: Using the Entity Framework tools that can connect to Oracle, you need to install a plug-in package before use, my vs is 2013, the plug-in package at the following address:

Http://www.oracle.com/technetwork/cn/topics/dotnet/downloads/odacmsidownloadvs2013-2756823-zhs.html

Once installed, we can use EF to connect to the Oralce database and connect it as follows:

Then we pull the table we need to use in the UI screen, note that when using EF, we have to set the table primary key , so we need to set the ID of the users table as the primary key, otherwise there is no way to pull the table object.

The code is as follows:

  #region Oracle EF         Console.WriteLine ("Oracle EF mode ");          New entities ();          var users = db. USERS. ToList ();          foreach (var in users)         {             Console.WriteLine (" name:" + item.name);         }      #endregion

Execution Result Screen:

The way I know how to connect to Oracle is what I'm currently aware of, and there are other ways that I might not be clear about it for the time being.

Reference article:

http://blog.csdn.net/zz155666/article/details/53163115

Https://www.cnblogs.com/gguozhenqian/p/4262813.html

C # connects Oracle in many ways.

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.