SQL Server connection from xml file

0

my question is this I must pass my current connection string, which is this:

   internal Clsconexion()
    {
        cadena = ConfigurationManager.ConnectionStrings["ConSicap"].ConnectionString;
    }

But they ask me to improve it and pass it to this new chain format:

//******************************************************************
strArchivoXml = Convert.ToString(ConfigurationManager.AppSettings.Get("RutaXmlDatos"));
        // Obtiene la ruta de acceso del archivo ejecutable que inicia la seccion.

        try
        {
            objDocumento.Load(strArchivoXml);
            objNode = objDocumento.SelectSingleNode("//Servidor");
            strServidor = objNode.InnerText;
            objNode = objDocumento.SelectSingleNode("//Basedatos");
            strBaseDatos = objNode.InnerText;
            objNode = objDocumento.SelectSingleNode("//Usuario");
            strUsuario = objNode.InnerText;
            objNode = objDocumento.SelectSingleNode("//clave");
            strClave = objNode.InnerText;
            objNode = objDocumento.SelectSingleNode("//SeguridadIntegrada");
            strSeguridadIntegrada = objNode.InnerText;

            if (strSeguridadIntegrada == "Si")
            {
                strStringConexion = "Data Source=" + strServidor + ";Initial Catalog=" + strBaseDatos + "; Integrated Security = SSPI";
            }
            else
            {
                strStringConexion = "Data Source=" + strServidor + ";Initial Catalog=" + strBaseDatos + ";User Id =" + strUsuario + "; Password=" + strClave + ";";
            }

            objDocumento = null;
            return true;
        }
        catch (Exception ex)
        {
            strError = ex.Message;
            objDocumento = null;
            return false;
        }

My question is how to create the file xml that I call from my App.config , what format should I have since I have never worked with these chains, I would appreciate your help.

Thank you very much in advance!

    
asked by Brian Velez 14.11.2018 в 14:44
source

1 answer

0
  

What is sought is that nowhere in the program are the credentials and Ip of the DB server that is why they want it in a file outside the source code.

One option is to move the connectionStrings section out of the App.config to another configuration file, so you can continue using ConfigurationManager.ConnectionStrings without further complication.

in App.config:

<!-- el resto de la configuración igual -->
<connectionStrings configSource="connections.config"/>

we create another file called "connections.config":

<?xml version="1.0" encoding="utf-8" ?>
<connectionStrings>
  <add name="test" connectionString="..."/>
</connectionStrings>

This would be the entire content of the file, it can only have the section that it will replace.

Finally, configure so that when the project is compiled the new configuration file is copied to the same folder so that it works:

    
answered by 14.11.2018 в 16:37