Search code examples
c#databasesql-server-ceconnection-string

Keyword not supported: 'attachdbfilename' - MDF Database File (C#)


I trying connect to my .MDF file but I am not able to accomplish it. I tried various variations of connection strings but still getting this same error.

This my connection string from app.config

<connectionStrings>
    <add name="DBConnection"
         connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\iseo-db.mdf;Integrated Security=True"
         providerName="System.Data.SqlClient" />
</connectionStrings>

and this is how I call this connection string

private string connection = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ToString();

SqlCeConnection con = new SqlCeConnection(connection);

When every I try execute I get a exception saying

Keyword not supported: 'attachdbfilename'

I would really appreciate any help on this problem.


Solution

  • As far as i know, SQL-Server CE does not use mdf files, rather than sdf files. Futhermore you don't have to use the property AttachDbFilename, then simply use the property Data Source=....

     <add name="DBConnection"
          connectionString="Data Source=|DataDirectory|/iseo-db.sdf;Integrated Security=True"
          providerName="System.Data.SqlServerCe.4.0" />
    

    Some thing like this should do the trick.

    EDIT

    According to @marc_s, the data provider also has to be changed to: System.Data.SqlServerCe.4.0