Welcome to Ultra Developers.

the company that developes your success

Read Blog


How to: Test that an Application can establish a Connection to Oracle Database using C#

How to: Test that an Application can establish a Connection to Oracle Database using C#

Sometimes your application connects to database for querying, inserting and deleting data from tables. You need to make sure that the database is running and accept incoming queries across the network. For Oracle Databases you can use tnsping command to test that the database is running as follows:

tnsping [Net Service Name] [Count]

Test Connection using tnsping

But if you want to make sure that your application can establish a connection to Oracle Database?

Using the Code:

To make sure that your application can establish a connection to database, follow these steps:

  1. Create a new windows application using Visual Studio 2005/2008/2010.
  2. Rename Form1 to ConnectionForm.
  3. Add a TextBox to the ConnectionForm and rename it to DataSourceTextBox.
  4. Add another TextBox to the ConnectionForm and rename it to UserIdTextBox.
  5. Add another TextBox to the ConnectionForm and rename it to PasswordTextBox and set the PasswordChar property to * character.
  6. Import the System.Data and System.Data.OracleClient namespaces using the following statement:
using System.Data;
using System.Data.OracleClient;
  1. The System.Data namespace provides access to classes that represent the ADO.NET architecture. ADO.NET lets you build components that efficiently manage data from multiple data sources.
  2. The System.Data.OracleClient namespace is the .NET Framework Data Provider for Oracle.
  3. Add an ErrorProvider component to the ConnectionForm and name it errorProvider. The Error Provider provides a user interface for indication that a control on the form has an error associated with it.
  4. Add a Button to the ConnectionForm and name it TestConnButton and double click the TestConnButton in the designer to create the Click event handler in the code.
  5. Add the following code to the TestConnButton Click Event Handler:
private OracleConnection oracleConnection;

private void TestConnButton_Click(object sender, EventArgs e)
{
    try
    {
        errorProvider.Clear();
        if (string.IsNullOrEmpty(DataSourceTextBox.Text))
        {
            errorProvider.SetError(DataSourceTextBox, "Required");
            return;
        }

        if (string.IsNullOrEmpty(UserIDTextBox.Text))
        {
            errorProvider.SetError(UserIDTextBox, "Required");
            return;
        }

        if (string.IsNullOrEmpty(PasswordTextBox.Text))
        {
            errorProvider.SetError(PasswordTextBox, "Required");
            return;
        }
 
        connString = "Data Source={0};Persist Security Info=True;"
            + "User ID={1};Password={2};Unicode=True";
        connString = string.Format(connString, DataSourceTextBox.Text,
            UserIDTextBox.Text, PasswordTextBox.Text);

        oracleConnection = new OracleConnection(connString);
        oracleConnection.Open();

        MessageBox.Show("Test Connection Succeeded", "Success",
            MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (OracleException ex)
    {
        MessageBox.Show(ex.Message, ex.Message,
            MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    catch (InvalidOperationException ex)
    {
        MessageBox.Show(ex.Message, ex.Message,
            MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, ex.Message,
            MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        oracleConnection.Close();
    }
}
  1. In the above code:

    • First of all we clear any error in the errorProvider component by calling the Clear method of the errorProvider.
    • We then check that the user enters a value in the DataSourceTextBox and we notify him if no value is entered. You will see a blinking red image beside the text box if no value is entered and if you stop the mouse over it a tooltip is displayed with a required text.
    • We then check that the user enters a value in the UserIdTextBox and we notify him if no value is entered. You will see a blinking red image beside the text box if no value is entered and if you stop the mouse over it a tooltip is displayed with a required text.
    • We then check that the user enters a value in the PasswordTextBox and we notify him if no value is entered. You will see a blinking red image beside the text box if no value is entered and if you stop the mouse over it a tooltip is displayed with a required text.
    • We create a string variable that holds the connection string that contains placeholders for data source, user Id and password.
    • Replace the place holders of the connString instance using string.Format method.
    • Create an instance of the OracleConnection class, name it oracleConnection and pass the connection string to the constructor of the OracleConnection class.
    • We then call oracleConnection open method to establish a connection to the database. This stage determines if the connection is established or not.
    • If the connection is established a message box will be displayed stating that “Test Connection Succeeded “.
    • If the connection could not be established a message box will be displayed stating that there is an exception and the exception message is displayed. You can use the exception message to review the error and test again.
    • We then call oracleConnection close method to close the database connection and releases any resources associated with the connection. The Close method rolls back any pending transactions. It then releases the connection to the connection pool, or closes the connection if connection pooling is disabled.
  2. Now compile and run the application.

  3. Enter any TNS Service Name in the DataSourceTextBox, user name in the UserIdTextBox and password in the PasswordTextBox and click on the TestConnButton.

  4. You will be notified if the connection to the database is established or not.

Test Database Connection is Successfull

Test Database Connection is failed

Now you have an application that can make sure that your application can test establishing a connection to Oracle Database or not.

Similar Posts