[英]Data Source in connection string - Setup project
我正在為C#桌面應用程序創建一個安裝項目。 應該在訪問數據庫的連接字符串中寫入什么數據源?我應該在解決方案項目中放置數據庫文件的位置?
假設您正在使用VS安裝項目,則需要添加訪問數據庫文件作為內容並將其放置在應用程序目錄中。 要在配置文件中指定位置,您需要編寫一個自定義操作,以相應地修改連接字符串。 以下示例是一個安裝程序類,該類在安裝階段(未測試)之后設置連接字符串:
[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);
}
}
或者,您可以使用在util
擴展中具有XmlFile
實用程序的Wix
,無需編寫自定義操作即可為您完成此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.