简体   繁体   English

在安装时存储 mdf 文件的更好方法是什么

[英]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.我尝试了两种解决方案。

  1. Include mdf file to installation folder via exe file.通过 exe 文件将 mdf 文件包含到安装文件夹中。 When I do that , program starts and executes fine, database works untill i have update or insert commands, exception is thrown which says that db is read-only.当我这样做时,程序启动并执行正常,数据库工作直到我有更新或插入命令,抛出异常表明 db 是只读的。 I tried running with administrator or changing privilegies, but still same problem.我尝试与管理员一起运行或更改权限,但仍然存在同样的问题。
  2. Save mdf file to mydocuments or appdata That way I create special folder via VS installer and add mdf file there, but problem is I don't know how to make relative connection string, because it is set to datadirectory.将 mdf 文件保存到 mydocuments 或 appdata 这样我通过 VS 安装程序创建特殊文件夹并在那里添加 mdf 文件,但问题是我不知道如何制作相对连接字符串,因为它被设置为数据目录。
<?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.

相关问题 将信息存储在文件中-用什么方式? - Store information in file - What way? 有条件删除文件的更好方法是什么 - What is better way to delete file with condition 在XML中存储文件名的正确方法是什么? - What is the proper way to store a file name in XML? 在C#中使用.mdf数据库的正确方法是什么? - What is the correct way to work with an .mdf database in C#? 这是将数据库(.mdf文件)连接到Visual Studio项目的正确方法吗? - Is this the correct way for connecting database (.mdf file) to visual studio project? 保存复选框/文本框状态的更好方法是什么? 将这些保存在“ .settings”文件中更好吗? - What is the better way to save state of checkboxes/textboxes? Saving these in “.settings” file is better? 什么是更好的验证方式/地点? - What is the better way/place for validation? 哪种方法更好地使用异常? - What way is better to use exceptions? 是否有比使用StringCollection更好的方法来在应用程序配置文件中存储字符串列表? - Is there a better way to store a list of strings in an applications config file than using a StringCollection? 更好的做法是:存储字符串或使用关系? - What is better practice: to store a string or use relations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM