Saman
Saman

Reputation: 469

Where to put SDF file in the Solution and how to write the "data source" in App.config?

I'm using a SQL Server CE .sdf database file and I configure this database in the app.config using this <connectionStrings> tag:

<connectionStrings>
    <add name="ReviewsDBConnection" 
         connectionString="data source=&quot;E:\GoogleDrive\bin\Debug\Data\ReviewsDB.sdf&quot;;password=123" 
         providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>

But whenever I move my project (the whole solution) to another PC, I get an error which says the ReviewsDB.sdf file path is not correct.

What should I write for data source that won't need to be changed when I move the solution to another PC? I'm using Telerik Open Access to get access to database.

Upvotes: 1

Views: 2025

Answers (1)

Doroteya Agayna
Doroteya Agayna

Reputation: 161

As far as I understand you, you need to set relative path to the database in the connection string. If this is the case, with Telerik Data Access and SQL Server CE you can achieve it like this:

  1. Include the .sdf file in the data access layer project.
  2. Set the Copy to Output Directory property of the .sdf file to Copy always.
  3. Provide the connection string like this:

    <connectionStrings>
      <add name="ReviewsDBConnection" 
           connectionString="data source=|DataDirectory|\ReviewsDB.sdf;password=123" 
           providerName="System.Data.SqlServerCe.4.0" />
    </connectionStrings>
    
  4. Run the app for a test.

Upvotes: 2

Related Questions