In C #, do not install Oracle clients how to connect to an Oracle database

Source: Internet
Author: User

The advantages of this method: 1, the integration of Oracle client within the program, no need to configure users themselves

The disadvantage of this method: 2, increase the volume of the package (Oracle Instant client itself has a few 10 megabytes size)

Here's how it's implemented.

  • 0. First, download the corresponding version of Oracle Instant client package from Oracle website, here http://www.oracle.com/technetwork/database/features/ Instant-client/index-097480.html because it is a Windows program, only need to download Win32 or 64 version on the line.
  • 1, download the. zip package, the following files should be extracted: (I use the 11G version, the other version may be slightly unused)
  • 2. Create a new "Oracle" folder in your project root directory (in fact, name and location), then copy the above files inside and add this folder and files to Visual Studio. Make sure you see them in your solution.
  • 3. In the visual Studio IDE's Solution Explorer, open the Oracle folder, select all the files in it, and in the "Properties" "Copy to Output directory", set "copy if newer", so that when compiling or publishing the program, Oracle folders will not be able to find the Oracle library when it is deployed to the location where the EXE resides.
  • 4, according to most of the online tutorials, this time you need to add something inside the widnows environment variable. In fact, this step is not necessary, because considering the different customer environment, it is possible that the user does not have permission to operate on his computer, then we put the set Oracle environment variables in the program to do. Before you use Oracle after initializing your program, you need to add the following code:string oraclePath = System.Windows.Forms.Application.StartupPath + @"\oracle"; Environment.SetEnvironmentVariable("PATH", oraclePath,EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("NLS_LANG", "SIMPLIFIED CHINESE_CHINA.ZHS16GBK", EnvironmentVariableTarget.Process);Explain the above code:string oraclePath = System.Windows.Forms.Application.StartupPath + @"\oracle";This sentence gets the location of the Oracle Drive folder, which is where oci.dll is placed.Environment.SetEnvironmentVariable("PATH", oraclePath,EnvironmentVariableTarget.Process);This sentence sets the environment variable "PATH", writes the folder where the Oracle driver resides, and the third parameter indicates that the PATH only works in the current process and does not modify the computer itself. Note: If you use some external programs, there are other path variables to set up, join here. See. NET related documentation for specific methods.Environment.SetEnvironmentVariable("NLS_LANG", "SIMPLIFIED CHINESE_CHINA.ZHS16GBK", EnvironmentVariableTarget.Process);This sentence sets the language and character set that Oracle uses in the communication process. My project uses the above character set, which corresponds to your project and can be queried in the Oracle database using SQL statements. This language and character set must be consistent with the server, or it may be garbled or even unable to connect. The following SQL statement may be used://select userenv(‘language‘) from dual; 查询服务端字符集,用来设置上面的参数。 //select * from nls_database_parameters;//服务器字符集 //select * from nls_instance_parameters;//ora文件定义字符集 //select * from nls_session_parameters; //会话字符集
  • 5, theoretically, after the above settings, your program can use the built-in to the EXE directory of the Oracle driver, here to note there is the way to connect the database, the above modification does not guarantee that you can use the TNS name to connect, so the connection string to do the following changes:

    Modify connectionstring in app. config to Persist Security Info=True;User ID=数据库用户名;Password=数据库密码;Unicode=True;Data Source= (DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 服务器IP地址)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = 服务器上的数据库实例名))); replace the "data source" part directly with the TCP/IP string (usually the data source is the one that writes the TNS name)

  • 6. Well, run your program and try it.

If you have a TNS name to access the Oracle database, do the following.

    • A. Add Tnsnames.ora to the program's Oracle folder and write the name of TNS into the inside.
    • B. Where the program sets environment variables, addEnvironment.SetEnvironmentVariable("TNS_ADMIN", oraclePath,EnvironmentVariableTarget.Process);
    • C. The TNS name should be available in the connection string

The overall code is as follows:

 //This sentence gets the location of the Oracle Drive folder, which is where oci.dll is placed.             stringOraclepath = System.Windows.Forms.Application.StartupPath +@"\orclconn"; // //This sentence sets the environment variable "PATH", written to the folder where the Oracle driver resides,// //The third parameter indicates that this path only works in the current process and does not modify the computer itself. // //Note: If you use some external programs, there are other path variables to set up, join here. See. NET related documentation for specific methods. ///when the system also uses the environment variables of other software, it needs to get the environment of the native computer and use it together with the Oracle environment variable.             strings = environment.getenvironmentvariable ("PATH", Environmentvariabletarget.machine); Environment.setenvironmentvariable ("PATH", S +";"+Oraclepath, environmentvariabletarget.process); // //this sentence, set the language and character set that Oracle uses in the communication processEnvironment.setenvironmentvariable ("Nls_lang","simplified Chinese_china. ZHS16GBK", environmentvariabletarget.process); // //add Tnsnames.ora to the program's Oracle folder and write the name of TNS in the inside. // //B. Where the program sets environment variables C. The TNS name should be available in the connection stringenvironment.setenvironmentvariable ("Tns_admin", Oraclepath, environmentvariabletarget.process);

In C #, do not install Oracle clients how to connect to an Oracle database

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.