How to store database link strings in the Registry

Source: Internet
Author: User

The connection string of the database is required for database development. We connect to the database through the link string and perform various database operations. So where can I store the link string? There is no final conclusion. In ASP system development, there is usually a Conn. ASP contains files or if it is com, it may be closed to com ,. NET development, most of them choose to exist in the web. in conifg, someone is also stored in another separate file. This article provides a solution to store the database link information in the registry. Maybe you want to ask why you want to store the data in the registry? I think the minimum benefit is security. The Registry has a high access permission. Generally, remote services do not have the permission to operate the Registry without obtaining an administrator account, therefore, the account information used to connect to the database is much safer.

1. Create the corresponding keys and values in the registry:

There are three ways to add the required information to the registry:

1. manual method: Start-> Run-> regedit, and then create the required key and value at the corresponding position.

2. Run a registry RegistrationProgram:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE \ SOFTWARE \ townsite]

"Town" = "Server = localhost; database = town; user id = sa; Pwd = pass"

SetCodeSave it as a. reg file, and double-click it to add the corresponding information at the corresponding location.

3. Program Implementation (C #):

Registrykey regkey = registry. localmachine. createsubkey ("Software \ townsite ");

Regkey. setvalue (dbname, "Server = Hi4-kelvin-yu; database = test; user id = sa; Pwd = pass ");

Note: C # provides classes and methods for processing the Registry. You must first introduce the namespace Microsoft. the Win32 and createsubkey Methods automatically detect whether there are any corresponding key values. If not, they are created. If yes, an instance of registrykey that represents the key is returned. if you can confirm that there is a key value, you can also open it with opensubkey. Dbname is the value name, which is passed through variables.

Ii. Read key values:

Reading key values is very simple

Registrykey regkey = registry. localmachine. opensubkey ("Software \ townsite", false );

Connectionstring = regkey. getvalue (dbname). tostring (). Trim ();

Note: Read the value information from the corresponding key, and then get the database link string assigned to a variable or attribute, and then you can connect to the database.

Iii. One Note:

If you use a program to create the key value of the registry, make sure that the ASPNET user has the permission to operate the registry. The method is as follows: "Start-> Run", enter the command, "regedt32 ", find "system-> CurrentControlSet-> services-> EventLog", select "security-> permission-> Add", and find the "ASPnet" user on the local machine, add and give the read permission. After adding it, there will be an "aspnet_wp account" in the directory"

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.