[英]What is the better way to store mdf file with installation
I made a perfect application and I have to make insallation for that app.我做了一个完美的应用程序,我必须为该应用程序进行安装。 But i have a localDB and there is my problem, how, and where to store mdf file.
但是我有一个 localDB 并且有我的问题,如何以及在哪里存储 mdf 文件。 I had searched about it on google but I haven't arrived at a solution yet.
我已经在谷歌上搜索过它,但我还没有找到解决方案。
I have tried two solutions.我尝试了两种解决方案。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Test1" connectionString=" Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Artikli.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Where other programs store their databases?其他程序在哪里存储他们的数据库? What is the best way to do it?
最好的方法是什么?
You should set up your connectionstring programmatically in some special folder in which the user will have access, like this:您应该在用户可以访问的某个特殊文件夹中以编程方式设置您的连接字符串,如下所示:
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\your folder\your app\your database.mdf";
string connectionString = string.Format("Data Source = {0}; Integrated Security = True; providerName = System.Data.SqlClient;", path);
..and when the program starts you should verify if this database is already saved in the specified folder, if not, you can copy the database from installation folder to there. ..并且当程序启动时,您应该验证此数据库是否已经保存在指定的文件夹中,如果没有,您可以将数据库从安装文件夹复制到那里。
File.Copy(Application.StartupPath + @"\your database.mdf", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\your folder\your app\your database.mdf");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.