0

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.

2
  • Do you need to provide a .sdf file with data already in it? Or could you just use the SqlCeEngine.CreateDatabase() call to create an empty .sdf database for you? Commented Aug 21, 2012 at 5:10
  • ya i need to provide .sdf file with data, can you help me? Commented Aug 21, 2012 at 7:45

1 Answer 1

1

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"

Sign up to request clarification or add additional context in comments.

2 Comments

return Path.Combine(baseDir, @"Data Source=D:\\Code Bunch\\Visual Studio 2010\\project\\project\\bin\\Debug\\database.sdf"); well should it be like that??
- no, your example is hardcoded. This method is defined to acquire full path to database depending on your application start up path. Example: your DB located on C:\DataBase.sdf. Your application is installed on C:\Program Files\Application\App.exe. Use Path.Combine(baseDir, @"\..\..\..\DataBase.sdf") to get full path to your DB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.