0

Hi I have connectionstring.xml which store in my localuserAppdataPath it stored a connection from my Database i would like to read it to a class dynamically so that i can call or load it anywhere in other form

here is the data in connectionstiring.xml

<ConnectionStrings>
  <SqlConnectionString>Data Source=TEST-PC;Initial Catalog=DTR_Test;Integrated Security=False;User ID=sa;Password=@@@123</SqlConnectionString>
  <DatabaseName>DB_Test</DatabaseName>
</ConnectionStrings>


private void btnFinish_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(cs))
        {

            if (goodCon == true)
            {

                string fname = Path.GetDirectoryName(Application.LocalUserAppDataPath) + "\\ConnectionStrings.xml";

                XmlTextWriter writer = new XmlTextWriter(fname, null);
                writer.Formatting = Formatting.Indented;
                WriteConnectionStrings(writer, cs, osb);
                writer.Close();

                this.Close();

            }
            else
            {

                this.Close();
            }
        } 
    }
    public void WriteConnectionStrings(XmlWriter writer, string sqlcon, string oledbcon)
    {
        writer.WriteStartElement("ConnectionStrings"); 
        writer.WriteElementString("SqlConnectionString", sqlcon);
        writer.WriteElementString("DatabaseName", dbname);
        writer.WriteEndElement();
    }
Jon P
  • 19,442
  • 8
  • 49
  • 72
  • 1
    Is there a reason for not using the inbuilt connection string manager and app.config (or web.config) : https://www.codeproject.com/Tips/416198/How-to-get-Connection-String-from-App-Config-in-Cs – Jon P Jul 09 '19 at 04:07
  • This url may help your problem. [ur](https://stackoverflow.com/questions/1189364/reading-settings-from-app-config-or-web-config-in-net) – Hikmat Fauzy Jul 09 '19 at 04:15
  • 1
    @reygieprieto , you really should mention that in your actual question then as the connection string manager is very much the go to here and you get a whole heap of added benefits with the ability to encrypt/decrypt connection strings in app/web.config . Now that we have that out of the way, what happens or what doesn't happen when you try to implement the above code? – Jon P Jul 09 '19 at 05:19
  • the above code create a connectionstring.xml file which the xml file contains ' Data Source=TEST-PC;Initial Catalog=DTR_Test;Integrated Security=False;User ID=sa;Password=@@@123 DB_Test ' then i what i want to do is read that connection from xml file to a class – reygie prieto Jul 09 '19 at 06:34

0 Answers0