65.9K
CodeProject is changing. Read more.
Home

Multiple Database Connection File Manager

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.40/5 (3 votes)

Jul 31, 2008

CPOL

2 min read

viewsIcon

32940

downloadIcon

1336

Utility to manage multiple DB connection strings for an application.

Introduction

This application is intended to be integrated with an existing program, or become the shell of a new project that will utilize multiple database connections to Oracle and SQL Server database platforms.

Background

Many applications use a database as a backend source of data. In doing so, connecting to multiple databases is required, especially from a QA perspective. Customers also enjoy the ability to edit connections without having to edit XML or text configuration files due to the complexity of the files and lack of technical knowledge. I created this originally as a shell for a Data Generation utility for a software package I am testing. I ended up using an existing DAL that we had, so I shelved this. I decided that the effort I put into may save someone else time, so here it is.

Using the code

The code is straightforward in most areas, and is documented pretty well. I will say the XML / XPath portion is a little more complex. I want to thank Michael Chao as I used one of his programs from CodeProject as a starting base for editing the XML connection file. His article can be found here:

Here is a snippet for creating a new connection (string):

// Here is an example of the Create Connection Method using XML
private void CreateConnectionInXML()
{
    string FileNameNew = DataConnection.ConnectionFileLocation;
    
    // Determine Selected DB Type and set variable to write to xml
    int dbtypenew;                       
    if (comboBoxDBType.SelectedIndex == 0)
        dbtypenew = 1;
    else
        dbtypenew = 2;

    try
    {
        // Create an XML Reader, Open Document, Read, Close Reader
        XmlTextReader reader = new XmlTextReader(FileNameNew);
        XmlDocument docnew = new XmlDocument();
        docnew.Load(reader);
        reader.Close();
        XmlNode currNode;

        XmlDocumentFragment docFrag = docnew.CreateDocumentFragment();
        docFrag.InnerXml = "<connection>" +
            "<title>" + textBoxConnTitle.Text + "</title>" +
            "<server>" + textBoxServerName.Text + "</server>" +
            "<database>" + textBoxDatabaseName.Text + "</database>" +
            "<dbtype>" + dbtypenew  + "</dbtype>" +
            "<username>" + textBoxUsername.Text + "</username>" +
            "<password>" + textBoxPassword.Text + "</password>" +
            "</connection>";
        
        currNode = docnew.DocumentElement;
        currNode.InsertAfter(docFrag, currNode.LastChild);
        //save the output to a file 
        docnew.Save(FileNameNew);
        this.DialogResult = DialogResult.OK;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Exception: {0}", ex.ToString());
        this.DialogResult = DialogResult.Cancel;
    } 
}

This program was written with Visual Studio 2008 for .NET 3.5. I am sure it could be modified to work with lower versions.

Here are some screenshots:

Main Form

Edit Form

Points of interest

I will say this is my very first coding project outside of school. I am still working on the main tool I am creating, and wish I could show it here, but cannot for business reasons. I hope this helps someone, and welcome any feedback as I am just getting started in the software development field.

History

  • Version 1.0.