如何在用户输入的App.Config文件中设置Connectionstring [英] How Do I Set Connectionstring In App.Config File Base On User Input

查看:87
本文介绍了如何在用户输入的App.Config文件中设置Connectionstring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



请问如何使用用户输入在app.config文件中动态设置connectionstring,



i试过这个:

Hello Everyone,

Please how do i dynamically set connectionstring in app.config file using user inputs,

i have tried this:

                StringBuilder Con = new StringBuilder( "Data Source=" );
                Con.Append( txtServer.Text );
                Con.Append( ";Initial Catalog=" );
                Con.Append( txtDatabase.Text );
                if ( String.IsNullOrEmpty( txtUsername.Text ) &&          String.IsNullOrEmpty( txtPassword.Text ) )
                    Con.Append( ";Integrated Security=true;" );
                else
                {
                    Con.Append( ";User Id=" );
                    Con.Append( txtUsername.Text );
                    Con.Append( ";Password=" );
                    Con.Append( txtPassword.Text );
                }
                string strCon = Con.ToString( );
                updateConfigFile( strCon );

public void updateConfigFile(string con)
        {
            //updating config file
            XmlDocument XmlDoc = new XmlDocument( );
            //Loading the Config file
            XmlDoc.Load( AppDomain.CurrentDomain.SetupInformation.ConfigurationFile );
            // XmlDoc.Load("App.config");
            foreach ( XmlElement xElement in XmlDoc.DocumentElement )
            {
                if ( xElement.Name == "connectionStrings" )
                {
                    //setting the coonection string
                    xElement.FirstChild.Attributes[2].Value = con;
                }
            }
            //writing the connection string in config file
            XmlDoc.Save( AppDomain.CurrentDomain.SetupInformation.ConfigurationFile );
            //XmlDoc.Save("App.config");
        }





它似乎没有给出预期的结果,请不要理解任何帮助。



It doesn't seem to give the desired result, Please any assistance will be appreciated.

推荐答案

请参阅此处 [ ^ ]


这篇关于如何在用户输入的App.Config文件中设置Connectionstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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