C#和SQL Server CE-不支持数据源关键字 [英] C# and SQL Server CE - Data Source Keyword Not Supported

查看:124
本文介绍了C#和SQL Server CE-不支持数据源关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下解决方案,但运气不佳:
如何更新C#中的app.config connectionstring数据源值?

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

我的代码是:

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>

我要去哪里错了?

推荐答案

我最终创建了自己的XML文件,该文件存储数据库字符串,如果该字符串无效或希望连接到其他数据库,则允许用户对其进行更新。我认为它比尝试更新app.config更好。

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.

这篇关于C#和SQL Server CE-不支持数据源关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆