C # and SQL Server CE - data source keyword not supported

I am trying to execute the following solution and not having much luck: How can I update the value of the app.config data source in C #?

The code I have is:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Because it an EF connection string it not a normal connection string // so we pull it into the EntityConnectionStringBuilder instead EntityConnectionStringBuilder efb = new EntityConnectionStringBuilder( config.ConnectionStrings.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.TestDBConnectionString"] .ConnectionString); // Then we extract the actual underlying provider connection string SqlConnectionStringBuilder sqb = new SqlConnectionStringBuilder(efb.ProviderConnectionString); // Now we can set the datasource sqb.DataSource = "|DataDirectory|\\TestDBa.sdf"; // Pop it back into the EntityConnectionStringBuilder efb.ProviderConnectionString = sqb.ConnectionString; // And update... config.ConnectionStrings.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.TestDBConnectionString"] .ConnectionString = efb.ConnectionString; config.Save(ConfigurationSaveMode.Modified, true); ConfigurationManager.RefreshSection("connectionStrings"); 

In my app.config file there is:

 <?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> </configSections> <connectionStrings> <add name="WindowsFormsApplication1.Properties.Settings.TestDBConnectionString" connectionString="Data Source=|DataDirectory|\TestDB.sdf" providerName="Microsoft.SqlServerCe.Client.4.0" /> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup></configuration> 

Where am I going wrong?

0
source share
2 answers

As a result, I created my own XML file, which stores the database row and allows the user to update it if it is invalid or if they want to connect to another database. I think this works better than trying to update app.config.

0
source

I have been doing SQL CE only for a few days, but if you are doing something with the SqlCE database, you probably want to use the SqlCe classes rather than Sql - try SqlCeConnectionStringBuilder ?

In addition, while | DataSource | supported in CE, the connection string you posted looks like examples I saw and used.

0
source

All Articles