C # string used to connect to the Oracle database
C # The string connecting to the Oracle database is as follows:
Data Source = orclsid_127.0.0.1; user id = Scott; Password = Scott;
It should be particularly noted what the value of data source is and where it is obtained (the last two can be seen here. I guess we all know what it means, and the next two won't be parsed ).
Data source: literally, it is the data source, which is found in the tnsnames. ora file under the installation directory of oracle. Instead of finding it in "Data Source (ODBC)" under "Administrative Tools" of the system. The tnsnames. ora file is in
Under "client_1/Network/admin/" under the Oracle Installation Project. For example, orclsid_127.0.0.1 is like this:
Orclsid_127.0.0.1 =
(Description =
(Address = (Protocol = TCP) (host = 127.0.0.1) (Port = 1521 ))
(CONNECT_DATA =
(Server = dedicated)
(SERVICE_NAME = orclsid_127.0.0.1)
)
)
Here, SERVICE_NAME = orclsid_127.0.0.1 is the network service name (SID _ host name) of the Oracle database instance ). If it is not changed during installation, the default value is orcl.
The following documents are reproduced:
Connection string and Object
Oracle connection strings and Oracle name parsing are inseparable. Suppose we define a database alias oradb in the tnsnames. ora file, as shown below:
Oradb =
(Description =
(Address_list =
(Address = (Protocol = TCP) (host = otnsvr) (Port = 1521 ))
)
(CONNECT_DATA =
(Server = dedicated)
(SERVICE_NAME = orcl)
)
)
The oradb alias defines the database address connection information of the client. To use the oradb alias defined in the tnsnames. ora file described above, you need to use the following syntax:
String oradb = "Data Source = oradb; user id = Scott; Password = tiger;"; // C #
If you want to see the code at a glance, but it is not complicated, you can modify the connection string so that you do not need to use the tnsnames. ora file. You only need to replace the alias with the statement that defines the alias in the tnsnames. ora file.
// C #
String oradb = "Data Source = (description ="
+ "(Address_list = (address = (Protocol = TCP) (host = orasrvr) (Port = 1521 )))"
+ "(CONNECT_DATA = (Server = dedicated) (SERVICE_NAME = orcl )));"
+ "User ID = Scott; Password = tiger ;";
As you can see above, the user name and password are embedded into the connection string in unencrypted text form. This is the easiest way to create a connection string. However, in terms of security, text encryption is not feasible. In particular, you need
Understand that compiled. NET application code is only a little safer than source code files in the form of unencrypted text. You can easily decompile. Net DLL and exe files to view the original unencrypted text content. (Encryption is actually the correct solution.
But this topic is too far behind our discussion here .)
Next, you must instantiate a connection object from the connection class. The connection string must be associated with the connection object.
Oracleconnection conn = new oracleconnection (oradb); // C #
Note: by passing the connection string to the constructor of the connection object (this constructor is overloaded), the connection string is associated with the connection object. Other overloads of the constructor allow the following alternative syntaxes:
Oracleconnection conn = new oracleconnection (); // C #
Conn. connectionstring = oradb;
After the connection string is associated with the connection object, use the open method to create the actual connection.
Conn. open (); // C #