如何使用数据目录在Visual Studio 2010中创建安装文件的过程中附加SQL Server数据库文件(.MDF) [英] How to attach SQL Server database file (.mdf) during creating of installation file in Visual Studio 2010 using data directory

查看:507
本文介绍了如何使用数据目录在Visual Studio 2010中创建安装文件的过程中附加SQL Server数据库文件(.MDF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个简单的簿记系统应用中的所有数据将被保存到数据库文件 bookdb.mdf 。当我试图创建安装文件,我不知道从哪里把我的数据库文件。此外,我需要改变我的连接字符串。

I developed a simple book entry system application where all the data will be saved to database file bookdb.mdf. When I tried to create installation file I don't know where to put my database file. Besides I need to change my connection string.

下面是我当前的连接字符串

Here is my current connection string

{"Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\Software\\Projects\\Visual Studio project\\Seminar Library CSE KU\\bookdb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"}

我曾尝试通过增加一个改变我的连接字符串应用程序配置文件的解决方案,并有我写

I have tried to change my connection string by adding a application configuration file to the solution and there I write

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="bookdb.mdf" providerName="System.Data.SqlClient" connectionString="Source=.\SQLEXPRESS;AttachDbFilename=E:\Software\Projects\Visual Studio project\Seminar Library CSE KU\bookdb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" />
   </connectionStrings>
</configuration>



但是,当我安装我的应用程序到其他计算机就说明数据库文件丢失。所以,请帮助我如何解决这个问题。

But when I install my application to other computer it shows database file missing. So please help me how to solve this problem

推荐答案

将数据库文件到用户的ApplicationData目录下的子目录。安装程序项目将知道如何放置MDF文件在那里,你可以设置你的连接字符串中的代码如下:

Put the database file into a subfolder of the users ApplicationData directory. The installer project will know how to place the mdf file there, and you can set up your connection string in code as follows:

string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename={0}\\Seminar Library CSE KU\\bookdb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

#if DEBUG
    connectionString = string.Format(connectionString, "E:\\Software\\Projects\\Visual Studio project\\");
#else
    connectionString = string.Format(connectionString, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
#endif

// Use your connection string here.

这篇关于如何使用数据目录在Visual Studio 2010中创建安装文件的过程中附加SQL Server数据库文件(.MDF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆