繁体   English   中英

更改app.config的连接字符串值

[英]change the connection string values the app.config

我正在尝试更改连接字符串值

connectionString =“Data Source = localhost; Initial Catalog = Tracker; Persist Security Info = false; User ID = sa; Password = p @ ssw0rd”

从我的用户界面。 有谁能帮助从Windows应用程序的用户界面更改设置?

从原始帖子上的评论主题来看,听起来OP需要枚举数据源并允许用户选择一个(并且可能存储该首选项)。 假设是这样...

  • 如果可能的话,应使用集成的Windows安全性来保护与数据库的实际连接。 这是一种最佳实践 集成安全性无需在任何地方存储凭据。

  • 如果目标是选择数据源,则使用C#列出环境中的所有数据库并不困难。

  • 用户的偏好/选择应存储在app.config以外的其他位置。 如果不涉及凭据,则注册表,隔离存储或XML文件都是有效选项。

ASP.NET提供了“ 网站管理工具”来查看和管理ASP.NET网站的配置设置。 这些配置设置存储在一个名为web.config的xml文件中。

web.config 是一个应用程序配置文件,用于定义ASP.NET应用程序的配置设置,例如连接字符串,身份验证,授权等。

该工具提供了易于使用的界面来编辑配置设置。 但是,当您必须手动进行更改时,此工具可以正常工作。 有时,我们可能需要在运行时对web.config进行更改。 例如:一个用户请求控制连接字符串,更改会话超时等等。 为此,ASP.NET提供配置API,以编程方式操作web.config中的配置设置。 API包含在System.ConfigurationSystem.Web.Configuration命名空间中。 实际上,内部网站管理工具使用相同的API来编辑配置设置。 System.Web.Configuration命名空间中的WebConfigurationManager类用于创建Configuration对象。 该对象用于读取和写入对web.config文件的更改。

另请参阅保护连接字符串

让我澄清一下,这不是您要的内容,而是通过阅读该内容,您将能够找到一些线索,并了解建议的内容和不建议的内容。

如果这是一个Windows应用程序,则您无需更改app.config文件即可更改活动数据库连接。 在博客上写过这篇文章

以下用户使用C#更改连接字符串的方法:

public void SaveConnectionString(DbInfo dbinfo, string path,string appConfigFile)
    {
        try
        {
            string configFile = Path.Combine(path, appConfigFile);
            var doc = new XmlDocument();
            doc.Load(configFile);
            XmlNodeList endpoints = doc.GetElementsByTagName("connectionStrings");
            foreach (XmlNode item in endpoints)
            {
                if (item.HasChildNodes)
                {
                    foreach (var c in item.ChildNodes)
                    {
                        if (((XmlNode)c).NodeType == XmlNodeType.Element)
                        {


                            var adressAttribute = ((XmlNode)c).Attributes["name"];
                            if (adressAttribute.Value.Contains("YourConStrName"))
                            {
                                if (dbinfo.dbType == dataBaseType.Embedded)
                                {
                                    ((XmlNode)c).Attributes["connectionString"].Value = SetupConstants.DbEmbededConnectionString;
                                    ((XmlNode)c).Attributes["providerName"].Value = SetupConstants.DbEmbededConnectionProvider;
                                }
                                else if (dbinfo.dbType == dataBaseType.Microsoft_SQL_Server)
                                {
                                    if (dbinfo.sqlServerAuthType == SqlServerAuthenticationType.SQL_Server_Authentication)
                                    {
                                       // ((XmlNode)c).Attributes["connectionString"].Value = string.Format(SetupConstants.dbConnStringwithDb, dbinfo.databaseAdress, SetupConstants.SqlDbName, dbinfo.userId, dbinfo.password) + "MultipleActiveResultSets=true;";
                                        ((XmlNode)c).Attributes["connectionString"].Value = string.Format(SetupConstants.dbConnStringwithDb, dbinfo.databaseAdress, dbinfo.DatabaseName, dbinfo.userId, dbinfo.password) + "MultipleActiveResultSets=true;";

                                    }
                                    else if (dbinfo.sqlServerAuthType == SqlServerAuthenticationType.Windows_Authentication)
                                    {
                                        //((XmlNode)c).Attributes["connectionString"].Value = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=true;", dbinfo.databaseAdress, SetupConstants.SqlDbName);
                                        ((XmlNode)c).Attributes["connectionString"].Value = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=true;", dbinfo.databaseAdress, dbinfo.DatabaseName);
                                    }
                                    ((XmlNode)c).Attributes["providerName"].Value = SetupConstants.DbSqlConnectionProvider;
                                }
                            }
                        }
                    }
                }

            }
            doc.Save(configFile);
            string exePath = Path.Combine(path, "EDC.Service.exe");
            InstallerHelper.EncryptConnectionString(true, exePath);
        }
        catch (Exception ex)
        {
            //TODO://log here exception
            Helper.WriteLog(ex.Message + "\n" + ex.StackTrace);
            throw;
        }
    }

添加下面的类DBinfo

public class DbInfo
{
    public DataBaseType dbType { get; set; }
    public SqlServerAuthenticationType sqlServerAuthType { get; set; }
    public string ConnectionString { get; set; }
    public string databaseAdress { get; set; }
    public string userId { get; set; }
    public string password { get; set; }
    public string Port { get; set; }
    public string DatabaseName { get; set; }
}

public enum DataBaseType
{
    Unknown = 0,
    Embedded = 1,
    Microsoft_SQL_Server =2,
}

public enum SqlServerAuthenticationType
{
    Windows_Authentication = 0 ,
    SQL_Server_Authentication =1
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM