none
connection string RRS feed

  • Question

  • Below is a code regading storing and retrieving connection strings.

    I would appreciate someone helping me understand this better.

    Thanks.

     

     

    string connStr = "";
    // get connection string in the connStr variable
    // Write connection string to xml file
    XmlDocument xmlDoc = new XmlDocument();
    XmlNode xn = xmlDoc.CreateNode(XmlNodeType.Element, "ConnectionString", "");
    xn.InnerText = connStr;
    xmlDoc.AppendChild(xn);
    xmlDoc.Save(@"C:\ConnectionString.xml");

    // ...
    // Read connection string from the text file
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(@"C:\ConnectionString.xml");
    XmlNode xn = xmlDoc.SelectSingleNode("ConnectionString");
    connStr = xn.InnerText;

    Friday, July 6, 2007 11:53 AM

All replies

  • Code Snippet

    // Connection string placeholder

    string connStr = "";

     

    // Save a connection string value in an Xml Document

     

    // Create a new instance of an XmlDocument in memory

    XmlDocument xmlDoc = new XmlDocument();

    // Create a new xml node of type element called "ConnectionString"

    XmlNode xn = xmlDoc.CreateNode(XmlNodeType.Element, "ConnectionString", "");

    // Set the inner text of the node to the connStr value

    // <node>This is inner text</node>

    xn.InnerText = connStr;

    // Append the node back into the Xml Document

    xmlDoc.AppendChild(xn);

    // Save the Xml Document to disk (serialize)

    xmlDoc.Save(@"C:\ConnectionString.xml");

     

    // Read a connection string value from an Xml Document

     

    // Create a new instance of an XmlDocument in memory

    XmlDocument xmlDoc = new XmlDocument();

    // Load the XmlDocument from disk (deserialize)

    xmlDoc.Load(@"C:\ConnectionString.xml");

    // Select the connection string node

    XmlNode xn = xmlDoc.SelectSingleNode("ConnectionString");

    // Set the string value to the node's inner text

    // <node>This is inner text</node>

    connStr = xn.InnerText;

     

    Friday, July 6, 2007 12:01 PM
  • Of course, a more approachable method is to utilise the App.Config file to store you connection string data.  Examples and discussion -> http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=147652&SiteID=1
    Friday, July 6, 2007 1:07 PM
  • Agreed.
    Wednesday, July 11, 2007 1:48 PM
  • np
    Wednesday, July 11, 2007 1:50 PM