【问题标题】:Modify Database connection string of web.config..?修改 web.config.. 的数据库连接字符串?
【发布时间】:2012-04-17 21:48:48
【问题描述】:

美好的一天!我想问一下,如果你知道如何修改 web.config.. 它位于不同的文件夹中.. 我尝试使用这种方法,不幸的是我不起作用..

// set Path to your config file
System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(sWebConfig);
// open web.config 
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
// display message 
Console.WriteLine("Updating wizardConnection string.. Please wait for a few minutes..");
// fetch WizardConnection database connection string 
var Wizardsection = (ConnectionStringsSection)configuration.GetSection("WizardConnection");
// assign new value to wizardConnection.. Please make sure you have the correct database server. Just update server location, if need
Wizardsection.ConnectionStrings["WizardConnection"].ConnectionString = string.Format(@"server={0};database={1};integrated security=SSPI", sDatabaseServer, sDatabase);
configuration.Save();

希望尽快收到您的来信..

问候,

链接

【问题讨论】:

  • 我认为 web.config 不能从应用程序中写入。

标签: c# database web-config connection-string


【解决方案1】:

您应该使用 webconfigurationmanager 打开您的 webconfig。

试试这个

        var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        config.ConnectionStrings.ConnectionStrings["ConnectString"].ConnectionString = string.Format(@"server={0};database={1};integrated security=SSPI", sDatabaseServer, sDatabase);

        config.Save();

【讨论】:

    【解决方案2】:

    实际上,您所做的将更改复制到包含二进制文件(调试、发布或任何自定义编译器配置)的文件夹的配置文件。这足以更改应用程序的连接设置。但是,如果你想修改 web.config 文件,那就是修改一个外部文件,你需要 File 操作。

    【讨论】:

      【解决方案3】:

      您可以为不同的构建配置使用不同的 web.config 文件,而不是更新 web.config。例如,您可以设置一个名为“Staging”的新构建配置并将其配置为使用修改后的配置文件。 看看Web.Config transformation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-05
        相关资源
        最近更新 更多