It is very convenient to use the mdf file embedded in the program in the project for SQL Server database tutorial development. It is convenient to release open-source projects. You can click it to run it without deployment, this is especially useful in teaching. Teachers do not need to detach database files to students or attach database files. The mdf file is embedded in the project. The instructor sends the lecture code to the students and the students can run the program as soon as they open it. This method is used in teaching.
In the asp tutorial. net program, you only need to put the mdf file in the app_data folder of the project, and use
Data source =. sqlexpress; attachdbfilename = | datadirectory | callcenter. mdf; integrated security = true; user instance = true
Make the connection string.
However, in the winform program, if you create an mdf file in the app_data folder of the project
Data source =. sqlexpress; attachdbfilename = | datadirectory | callcenter. mdf; integrated security = true; user instance = true
When the connection is established, the system will prompt that callcenter. mdf cannot be found. The original winform program will not find the mdf file in app_data. In the original asp.net tutorial, the value of datadirectory is the app_data path of the current project, and the value of datadirectory in winform is the path of the current project. Therefore, the mdf file in winform does not need to be placed in app_data, put it in the project root directory.
However, the new problem arises. When developing in winform using this method, sometimes the data or table structure in the mdf file in the project is changed, during the running, the data or table structure read by the program is not changed during the running, but sometimes the data inserted by the insert statement is lost during the debugging. After research, we found that the winform program is connected to the mdf file under bin/debug, rather than the mdf file in the project, which is different from the asp.net program. Each time a program is built, the mdf in the project will overwrite the mdf file under bing/debug, that is, there are two mdf files, and the mdf in the project is equivalent to the "source file ". Although you can partially solve the problem by modifying the "buildtoouput" attribute of the file, it is still not perfect.
There is a very direct idea: let the program connect to the mdf file in the project, rather than connect to the one under bin/debug.
After querying the information, find the modification method and add the following code at the beginning of the main function of the program. cs file:
String datadir = appdomain. currentdomain. basedirectory;
If (datadir. endswith (@ "bindebug ")
| Datadir. endswith (@ "binrelease "))
{
Datadir = system. io. directory. getparent (datadir). parent. parent. fullname;
Appdomain. currentdomain. setdata ("datadirectory", datadir );
}
Simple analysis of the principle: the value of datadirectory in the connection string is through appdomain. currentdomain. setdata has been assigned a value. If the current program directory is "bindebug" or "binrelease", it is considered to be running in the visual studio environment. Then, the project directory is assigned the key datadirectory. Since it is currentdomain. setdata, it is estimated that the database connection code in the non-default appdomain may not work (just speculation, not verification). This requires assigning a value when creating the sub-appdomain.
The above code has some potential bugs. For example, when the exe is officially run and put to a bindebug Directory, there will be problems, however, I think this attachdbfilename method will certainly not be used when the production environment is running. This method only exists in the development environment, so I will close one eye.