2013-02-25 40 views
0

我正在爲我的C#桌面應用程序創建安裝項目。 什麼數據源應該寫入訪問數據庫的連接字符串?以及我應該將數據庫文件放在解決方案項目中的位置?連接字符串中的數據源 - 安裝項目

+1

mssql,ms access,mysql? – spajce 2013-02-25 00:38:05

+0

它看起來像數據庫類型是「MS Access」。蓮花90,請澄清。 – Nate 2013-02-25 00:39:27

+0

@Nate MS-訪問數據庫 – 2013-02-25 13:21:36

回答

0

假設您使用的是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); 
    } 
} 

備選地,可以使用Wix其在util擴展,它會爲你沒有你的XmlFile效用編寫自定義操作。