C # connect to Oracle in multiple ways .,
If you don't talk nonsense, you can directly answer the question:
First, we first created a user named lisi under the Oracle database with the password lisi. Then, we created a table named "USERS" under this user and added three new data under this table.
Method 1: Use OleDb to connect to Oracle.
1 # region uses OleDb Mode 2 Console. writeLine ("OleDb mode"); 3 OleDbConnection oledbConnection = new OleDbConnection ("Provider = OraOLEDB. oracle; Data Source = MLDN; User Id = lisi; Password = lisi; "); 4 OleDbCommand oledbCommand = new OleDbCommand (); 5 oledbCommand. commandText = "select * from USERS"; 6 oledbCommand. commandType = CommandType. text; 7 oledbCommand. connection = oledbConnection; 8 oledbConnection. open (); 9 var oledbReader = oledbCommand. executeReader (); 10 while (oledbReader. read () 11 {12 Console. writeLine ("name:" + oledbReader [1]); 13} 14 oledbConnection. close (); 15 # endregion
Method 2: Use Oracle. managedDataAccess. client method. Before use, we need to find Oracle in Nuget. managedDataAccess and then install it. One of the advantages of this method is that you do not need to install the Oracle client on the client. This version was developed by Oracle and Microsoft has abandoned its System. data. oracleClient method (as described below). We recommend this method.
The Code is as follows:
# Region OracleConnection // OracleConnection connection = new OracleConnection ("Password = lisi; User ID = lisi; Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = 192.168.229.138) (PORT = 1521) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = ***) "); if no client is installed, you can use this method to Console. writeLine ("Oracle. managedDataAccess. client mode "); OracleConnection oralceConnection = new OracleConnection (" Data Source = MLDN; User Id = lisi; Password = lisi; "); oralceConnection. open (); OracleCommand oracleCommand = new OracleCommand (); oracleCommand. commandText = "select * from USERS"; oracleCommand. commandType = CommandType. text; oracleCommand. connection = oralceConnection; var listener lereader = oracleCommand. executeReader (); while (oracleReader. read () {Console. writeLine ("name:" + oracleReader [1]);} oralceConnection. close (); # endregion
Method 3: odbc
# Region odbc Console. writeLine ("odbc mode"); OdbcConnection odbcConnection = new OdbcConnection ("Driver = {Microsoft ODBC for Oracle}; Server = MLDN; Uid = lisi; Pwd = lisi ;"); odbcConnection. open (); OdbcCommand odbcCommand = new OdbcCommand (); odbcCommand. commandText = "select * from USERS"; odbcCommand. commandType = CommandType. text; odbcCommand. connection = odbcConnection; var odbcReader = odbcCommand. executeReader (); while (odbcReader. read () {Console. writeLine ("name:" + odbcReader [1]);} odbcConnection. close (); # endregion
Method 4: Use the System. Data. OracleClient method (this method is outdated and is not recommended ).
The Code is as follows:
1 # region 2 3 Console. writeLine ("System. data. oracleClient mode "); 4 System. data. oracleClient. oracleConnection clientConnection = new System. data. oracleClient. oracleConnection (); 5 clientConnection. connectionString = "Data Source = MLDN; User Id = lisi; Password = lisi;"; 6 System. data. oracleClient. oracleCommand clientCommand = new System. data. oracleClient. oracleCommand (); 7 8 clientCommand. commandText = "select * from USERS"; 9 clientCommand. connection = clientConnection; 10 clientCommand. commandType = CommandType. text; 11 clientConnection. open (); 12 var clientReader = clientCommand. executeReader (); 13 while (clientReader. read () 14 {15 16 Console. writeLine ("name:" + clientReader [1]); 17} 18 clientConnection. close (); 19 # endregion
Method 5: Use the Entity Framework tool that can connect to Oracle. before using the tool, install an plug-in package. My VS is 2013. The plug-in package is at the following address:
Http://www.oracle.com/technetwork/cn/topics/dotnet/downloads/odacmsidownloadvs2013-2756823-zhs.html
After the installation, we can use EF to connect to the Oralce database. The connection method is as follows:
Then we pull the table we need in the UI screen. Note that we need to set the table primary key when using EF, so we need to set the USERS table ID as the primary key, otherwise, you cannot pull the table object.
The Code is as follows:
# Region Oracle EF Console. writeLine ("Oracle EF mode"); Entities db = new Entities (); var users = db. USERS. toList (); foreach (var item in users) {Console. writeLine ("name:" + item. NAME) ;}# endregion
Execution result screen:
To sum up, I know how to connect to Oracle. As for other methods, I may not be quite clear at the moment.
References:
Http://blog.csdn.net/zz155666/article/details/53163115
Https://www.cnblogs.com/gguozhenqian/p/4262813.html