Open SQL Server CE database data from read only file system

c#-4.0, sql-server-ce

Solution

In order to open a SQL Server CE database file (SDF) on read-only media, you also need to add two additional parameters to the connection string

- Mode=Read Only

- Temp Path=[path]

You could do this as follows:

connectionString = String.Format(@"Data Source = {0}\{1};Mode = Read Only;Temp Path={2}", 
          dataBaseDirectory,
          dataBaseName, 
          System.IO.Path.GetTempPath());        

If you are getting your connection string from App.Config

see https://stackoverflow.com/a/10731515/19624

string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
connectionString += ";Mode = Read Only;Temp Path=" + System.IO.Path.GetTempPath()); 

Problem

I use a SQL Server CE database from my application. My program is located on a DVD. I cannot read data from the database on the DVD, I set SQL connection string mode to read only but it doesn't work (I just want to read data from db) ERROR: Opening a database as read-only requires a temp path to be specified. [ Db name = C:\Users\Ali\AppData\Local\Temp\Rar$EX52.280... ] please help!

Original source