我嘗試以下解決方案,運氣不好: How can i update app.config connectionstring Datasource value in C#?C#和SQL Server CE - 數據源關鍵字不支持
我的代碼是:
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");
我app.config
文件有:
<?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>
我在哪裏出錯了?
我最終做的是不使用app.config中的連接字符串,而是設置我自己的XML文件來讀取。我會嘗試一下你說的話,看看這是不是我出錯的地方。 – hshah
仍然是一樣的錯誤:( – hshah
對不起 - 猜猜問題在於別處。 –