How to solve the problem of two mdf files in the WinForm Program

Source: Internet
Author: User

It is very convenient to use the mdf file embedded in the program in the project for SQLServer database development. It is convenient to release open-source projects. You can click it to run it without deployment, especially in teaching, it is more convenient to use. teachers do not need to detach database files and send them to students, and students do not need to attach database files. When the mdf file is embedded in the project, the instructor sends the lecture code to the students, and the students can open it and run it. I used this method to explain in the training class of Chuanzhi podcast. net.

In the ASP.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 ASP.net, 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 (@ "\ bin \ Debug \")
| DataDir. EndsWith (@ "\ bin \ Release \"))
{
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 is assigned in the past. If the current program directory is "\ bin \ Debug \" or "\ bin \ Release \", it is considered to be running in the Visual Studio environment, take the project directory and assign it to 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 bin \ Debug directory, the problem may occur, but I think this AttachDbFilename method will definitely not be used when the production environment is running. This method only exists in the development environment, so I will close one eye.

[Reference from www.rupeng.com/forum/thread-11988-1-1.html]

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.