C # DSN operation

Source: Internet
Author: User
Tags dsn

You can obtain the DSN list in C #, or pop up the ODBC data source Manager to set it.

1. C # obtain the DSN list on the machine.

Use registrykey and Registry provided by Microsoft. Win32 to access the system registry to obtain the list of ODBC data services.

Obtain the system DSN as follows:

/// Create two registrykey classes. One Class points to the root path and the other class points to the sub-path.
Registrykey regrootkey;
Registrykey regsubkey;

/// Define root to point to the HKEY_LOCAL_MACHINE node of the Registry,

/// If you want to obtain the user DSN, you need to use registry. currentuser;
Regrootkey = registry. localmachine;

/// Define the registry sub-Path
String strregpath = @ "SOFTWARE \ ODBC. ini \ ODBC Data Sources ";
Regsubkey = regrootkey. opensubkey (strregpath );
String [] strdsnlist = regsubkey. getvaluenames ();

/// Close
Regsubkey. Close ();
Regrootkey. Close ();

2. the ODBC data source Manager is displayed.

Use the environment class provided by C.

/// Obtain the ODBC system path

String S = system. environment. systemdirectory. tostring () + "\ odbcad32.exe ";
/// Process provides access to local and remote processes and enables you to start and stop local system processes. Use this class to start ODBC dialog box

Process myp = new process ();
Myp. startinfo. filename = s;
Myp. Start ();

3. Differences between different DSN types

There are three DSN types:

User DSN:The data source can only be visible to users who have created the data source.
The ODBC user data source stores information about how to connect to a specified database provider. only visible to the current user, and can only be used on the current machine. the current machine is only valid for the current machine, rather than configuring the database on the local machine. it can configure the database on another machine in the LAN.

System DSN:This data source is visible to all users on the current machine.
The ODBC Data Source stores the information about how to specify the database provider connection. The system data is visible to all users on the current machine, including the NT Service. that is to say, the data source configured here can be accessed by users on this machine.

File DSN:This data source is visible to users with the same driver installed.
User DSN is used only by users. It can only be used on the current machine, but cannot be used by ASP. the system DSN allows all users to log on to a specific server to access the database. Any user with the permission can access the system DSN. when accessing a database in a web application, a system DSN is usually created. file DSN stores information with the suffix. in the DSN text file, the advantage is that it is easy to move.

The user DSN is only for the current user or a specific user; the system DSN is the underlying, for all users. Generally, if there are no special cases, we recommend that you use the system DSN for better versatility.

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.