Reputation: 744
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.
Upvotes: 0
Views: 794
Reputation: 5083
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="Data Source=|DataDirectory|\..\..\yourDB.sdf"" 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"
Upvotes: 1