更改连接字符串中C#.NET创建安装文件后, [英] Change Connection String After Creating Setup File in C#.NET

查看:96
本文介绍了更改连接字符串中C#.NET创建安装文件后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个C#Windows窗体应用程序,可以概括为用户工作充满某种形式和数据保存在Access数据库。我现在面临的问题是,我要送这是一个安装文件的人。我在想什么是代码一旦安装在其他计算机上并执行将使因Access数据库的连接字符串的错误,因为它不会与计算机相匹配。我知道,如果一个项目的分配,我可以把连接字符串中的app.config和每一个用户可以根据他/她的机器改变它。但正如我给一个安装文件,该如何解决这个问题。

I am creating a C# windows form applications, the working can be summarized as users fills some form and data is saved in Access database. Now the problem I am facing is that I have to deliver this as a setup file to someone. What I am thinking is that the code once installed on other computers and executed will give errors because of the connection string of Access db, as it will not match with that computer. I know that if a distribute projects I can put connection string in app.config and every user can change it according to his/her machine. But as I am giving a setup file how to solve this problem.

推荐答案

假设你用这个的ConnectionString部署您的app.config

Suppose you deploy your app.config with this connectionstring

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\yourFile.accdb;"

在WinForms应用程序的 | DataDirectory目录| 快捷方式代表你的应用程序的工作文件夹,但你可以在运行时它指向使用此代码更改。

In a WinForms application the |DataDirectory| shortcut represent your application working folder, but you can change at runtime where it points to using this code.

// appdomain setup information
AppDomain currentDomain = AppDomain.CurrentDomain;
//Create or update a value pair for the appdomain
currentDomain.SetData("DataDirectory", "Your user choosen path");



它无需进行硬编码的完整路径当中,有你已经发现,导致数问题解决在安装过程中。当然,你的设置应该在用户choosen路径提供您的数据库。

It eliminates the need to hard-code the full path which, has you have discovered, leads to several problems to resolve during install. Of course your setup should deliver your database in your user choosen path.

这篇关于更改连接字符串中C#.NET创建安装文件后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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