Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 连接字符串中的数据源-安装项目_C# - Fatal编程技术网

C# 连接字符串中的数据源-安装项目

C# 连接字符串中的数据源-安装项目,c#,C#,我正在为我的C#桌面应用程序创建一个安装项目。 access数据库的连接字符串中应该写入什么数据源?我应该将数据库文件放在解决方案项目中的什么位置?假设您使用的是VS setup项目,您需要将access数据库文件添加为内容,并将其放在应用程序目录中,例如。要在配置文件中指定位置,需要编写一个自定义操作,相应地修改连接字符串。 以下示例是在安装阶段(未测试)后设置连接字符串的安装程序类: [运行安装程序(true)] 公共部分类Installer1:System.Configuration.In

我正在为我的C#桌面应用程序创建一个安装项目。
access数据库的连接字符串中应该写入什么数据源?我应该将数据库文件放在解决方案项目中的什么位置?

假设您使用的是VS setup项目,您需要将access数据库文件添加为内容,并将其放在应用程序目录中,例如。要在配置文件中指定位置,需要编写一个自定义操作,相应地修改连接字符串。 以下示例是在安装阶段(未测试)后设置连接字符串的安装程序类:

[运行安装程序(true)]
公共部分类Installer1:System.Configuration.Install.Installer
{
公共安装程序1()
{
初始化组件();
this.AfterInstall+=新的InstallEventHandler(Installer1\u AfterInstall);
}
安装后无效Installer1_(对象发送器,InstallEventArgs e)
{
字符串sTargetDir=Context.Parameters[“TargetDir”];
字符串sAppConfig=Path.Combine(sTargetDir,“.exe.config”);
字符串sDBPath=Path.Combine(sTargetDir,.mdb);
XDocument doc=XDocument.Load(sAppConfig);
var elem=doc.Root.Element(“/configuration/connectionStrings/add[@name=”);
string connectionString=string.Format(“Provider=Microsoft.Jet.OLEDB.4.0;数据源={0};”,sDBPath);
元素SetAttributeValue(“connectionString”,connectionString);
单据保存(sAppConfig);
}
}

或者,您可以使用扩展名
util
中的
XmlFile
实用程序,该实用程序无需编写自定义操作即可为您执行此操作。

mssql、ms access、mysql?看起来数据库类型为“ms access”。Lotus 90,请澄清MS-Access数据库
[RunInstaller(true)]
public partial class Installer1 : System.Configuration.Install.Installer
{
    public Installer1()
    {
        InitializeComponent();
        this.AfterInstall += new InstallEventHandler(Installer1_AfterInstall);
    }

    void Installer1_AfterInstall(object sender, InstallEventArgs e)
    {
        string sTargetDir = Context.Parameters["TargetDir"];
        string sAppConfig = Path.Combine(sTargetDir, "<your app>.exe.config");
        string sDBPath = Path.Combine(sTargetDir, "<your db>.mdb");
        XDocument doc = XDocument.Load(sAppConfig);
        var elem = doc.Root.Element("/configuration/connectionStrings/add[@name='<your connection name>']");
        string connectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", sDBPath);
        elem.SetAttributeValue("connectionString", connectionString);
        doc.Save(sAppConfig);
   }
}