C# and SQL Server CE - Data Source Keyword Not Supported

不羁岁月 提交于 2020-01-17 04:24:23

问题


I am trying the following solution and not having much luck: How can i update app.config connectionstring Datasource value in C#?

The code I have is:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Because it's an EF connection string it's 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");

My app.config file has:

<?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?


回答1:


I've only been fiddling with SQL CE for a few days myself, but if you're doing anything with SqlCE database, you probably want to use SqlCe classes rather than Sql ones - try SqlCeConnectionStringBuilder?

Other than that, as long as |DataSource| is supported in CE the connection string you've posted looks like the examples I've seen & used.




回答2:


I ended up creating my own XML file which stores the database string and lets the user update it if it is invalid or if they wish to connect to a different database. I think it works out better than trying to update the app.config.



来源:https://stackoverflow.com/questions/12270326/c-sharp-and-sql-server-ce-data-source-keyword-not-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!