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.