Vb. NET read (database) configuration file

Source: Internet
Author: User

Before using the three-layer to do a log on the small demo, the problem is still a lot of, but also said the past. After that, you want to fix the string that connects the database, add it to the configuration file, and directly reference the configuration file in the code, read the data, and connect the database for subsequent work. However, in this simple configuration file, there is a small problem.

This is defined before the configuration file is used:

Public Class dallogin ...
Dim connstr as String = "Data source=192.168.24.126;initial catalog=student;" User Id=sa; pwd=123456 "
...
End Class
This can realize the connection of the database, but there is a very serious problem: if you want to change the database, by SQL Server into Access, Oracle, etc., or vice versa, you need to change the code, which undoubtedly caused a great deal of trouble, if the change is not good and there will be other bugs. So how to avoid it. Of course, the use of configuration files can effectively avoid these, but this is one of the methods, there are other ways, you can go to try. The following describes the operation of the configuration file.

First, add an "application configuration file" to the project, renamed to "App.config" (uppercase and lowercase), note that the profile needs to be created in the UI layer directory because the software can only be read at the UI layer, and if it is created at another layer, the software will not be able to find the profile and error prone. Then, in the created configuration file, write a string of the connection database, which is illustrated as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
	<appSettings>
		<add key = " ConnStr "value =" Data source=192.168.24.126;initial catalog=student; User Id=sa; pwd=123456 "></add>
		</appSettings>
    <system.diagnostics>
        <sources>
            <!--This section defines the My.Application.Log login configuration-->
        ...
        </sources> ...
    </system.diagnostics>
</configuration>
In the DAL layer, the code that reads the configuration file can write this:

Public Class dallogin ...
Dim connstr As String = System.Configuration.ConfigurationSettings.AppSettings ("ConnStr")
Dim conn As SqlConnection = New SqlConnection (connstr) ...
End Class
This can be said that the application of the configuration file to connect the database is completed, so that if the subsequent work needs to replace the database, then only need to open the config file Notepad, the connection string changes can be, without the redeployment, etc., to avoid a series of trouble.

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.