(Go to: Collection) How to store the database connection string in the registry

Source: Internet
Author: User

If we put the information connected to the database into the registry, we can greatly improve the security of the connection information. This article describes how to store the information of a connected database in the registry to improve the security of database connection information.

The connection string for the database is the information necessary to develop the database, and we connect to the database through the connection string and perform various database operations. This article provides scenarios for storing database connection information in the registry . Maybe you want to ask why you want to save it to the registry? I think the minimum benefit is security, access to the registry is very high, general remote services in the absence of an administrator account is not authorized to operate the registry, then the account used to connect the database information is much more secure.

First, in the registry to establish the corresponding keys and values:

There are three ways to add the information we need in the registry:

1, Pure manual method: Start and run->regedit, and then in the corresponding location to create a new key value .

2. Run a Registry registration program:

    1. Windows Registry Editor Version 5.00
    2. [Hkey_local_machine\software\townsite]
    3. "Town" = "server=localhost; " Database= Town; User id=sa; pwd=Pass "

Save the above code as one. REG file, and then double-click Run to add the appropriate information in the appropriate location.

3, program implementation (C #):

    1. RegistryKey RegKey = Registry.LocalMachine.CreateSubKey ("Software\\townsite");
    2. Regkey.setvalue (DbName, "server=Hi4-kelvin-yu; database=Test; User id=sa;  pwd=Pass ");

Description: C # has classes and methods for processing the registry, first to introduce a namespace Microsoft.Win32, the CreateSubKey method automatically detects that there is no corresponding key value, if not, and if so, returns an instance of RegistryKey that represents the key. If you can determine a key value, you can also open it with OpenSubKey. DBName is the value name, passed through the variable.

Second, read the key value:

Reading the key values is simple:

    1. RegistryKey RegKey = Registry.LocalMachine.OpenSubKey ("Software\\townsite", false);
    2. connectionString = regkey.getvalue (dbName). ToString (). Trim ();

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

31-Point Description:

If you are using a program to create key values for the registry, make sure that the ASPNET user has permission to operate on the registry as follows: "Start-to-run", enter the command, "regedt32", and locate "system->currentcontrolset-> Services->eventlog ", select" Security--rights-plus ", and then find the local" AspNet "user, add in and give Read permission, add in the directory will be more than a" aspnet_wp account. "

Knowledge: Quickly open registry regedit

Warning: name cannot be the same, connection statement cannot be the same

(go to: Bookmark) How to store the database connection string in the registry

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.