简体   繁体   中英

Embedding SQL Server CE database

I'm working with sql server ce database and C#, and I wondering how to really embed the database file so when I have finish installing the app I don't need to copy-paste the database file to the environment as I set in the following code :

 private void sambung()
    {
        string lokasifile = Environment.CurrentDirectory + "\\compactedition.sdf";
        string stringkoneksi = "Data Source = \"" + lokasifile + "\";Password = 'blablabla'; Encrypt = True";
        koneksi = new SqlCeConnection(stringkoneksi);
    }

Do you have any suggestion to make it possible? Thanks.

If you copy your database to root directory or upper you can use relative path like this:

public static string GetLogicBinDebug()
    {
        string baseDir = AppDomain.CurrentDomain.BaseDirectory;
        return Path.Combine(baseDir, @"..\..\database.sdf");
    }

Or you can keep connection string in app.config using relative path:

<connectionStrings>
<add name="dbEDMContainer" connectionString="metadata=res://*/dbEDM.csdl|res://*/dbEDM.ssdl|res://*/dbEDM.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;Data Source=|DataDirectory|\..\..\yourDB.sdf&quot;" providerName="System.Data.EntityClient"/>

Data Source=|DataDirectory|\\ will point to "bin" folder

** If you are asking about how to copy database file to your application folder, you can use this approach:

Include Database file to your project in Visual Studio, open file properties and set Build action to " Content ", Copy to output directory - " copy if newer " or " copy always "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM