News
  • Favorite
  • Facebook
  • Twitter
  • LinkedIn

Microsoft Press Pearson Education (InformIT)
Skip Navigation Links
Pearson Education (InformIT)
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]

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;

7.       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.

8.       The System.Data.OracleClient namespace is the .NET Framework Data Provider for Oracle.

9.       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.

10.    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.

11.   Add the following code to the TestConnButton Click Event Handler:

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 oracleConnection = new OracleConnection(connString);

        oracleConnection.Open();

 

        MessageBox.Show("Test Connection Succeeded", "Success",

            MessageBoxButtons.OK, MessageBoxIcon.Information);

 

        oracleConnection.Close();

    }

    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

    {

    }

}

a.       First of all we clear any error in the errorProvider component by calling the Clear method of the errorProvider.

b.      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.

c.       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.

d.      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.

e.      We create a string variable that holds the connection string that contains placeholders for data source, user Id and password.

f.        Replace the place holders of the connString instance using string.Format method.

g.       Create an instance of the OracleConnection class, name it oracleConnection and pass the connection string to the constructor of the OracleConnection class.

h.      We then call oracleConnection open method to establish a connection to the database. This stage determines if the connection is established or not.

i.         If the connection is established a message box will be displayed stating that “Test Connection Succeeded “.

j.        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.

k.       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.

 

12.   Now compile and run the application.

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

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

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

(69) Comments

Blog Message https://www.clickterminal.com/

This is one awesome article post.Much thanks again. Really Cool.


Blog Message http://www.bamncoaching.com

Thanks-a-mundo for the blog post.Really looking forward to read more. Much obliged.


Blog Message https://volunteer.cs.und.edu/csg/team_display.php?teamid=61624

Way cool! Some extremely valid points! I appreciate you writing this post and the rest of the website is extremely good.


Blog Message https://desensetize-my-soul.tumblr.com/

Im obliged for the article. Really Great.


Blog Message https://www.songoflove.org

Valuable Website I have been checking out some of your posts and i can claim clever stuff. I will definitely bookmark your blog.


Blog Message https://broken-heart-17.tumblr.com/

Hey, thanks for the blog article. Awesome.


Blog Message http://www.tarotgyan.com/best-astrologer-mumbai/

my authorization. Do you know any solutions to help prevent content from being stolen?


Blog Message http://www.hoardinghelpers.com

Wonderful opinions you ave got here.. I appreciate you discussing your perspective.. Fantastic views you might have here.. Definitely handy viewpoint, many thanks for giving..


Blog Message https://www.portlandbodyclinic.co.uk/fat-freezing

I thought it was going to be some boring old publish, but it really compensated for my time. I will post a link to this page on my blog. I am positive my visitors will find that very useful


Blog Message https://hammihan.com/post/16012136

Your style is really unique in comparison to other people I ave read stuff from. Thank you for posting when you have the opportunity, Guess I will just bookmark this site.


Blog Message http://edigitize.com

Really appreciate you sharing this blog article.Really thank you! Awesome.


Blog Message http://capitaltechies.com/non-profit-use-capital-techies-for-it-support-in-washington-dc/

Spot on with this write-up, I really suppose this web site wants way more consideration. I?ll most likely be once more to learn way more, thanks for that info.


Blog Message https://www.facebook.com/6ombagency/

I think this is among the most significant information for


Blog Message http://www.lesflyers.be/forum/profile.php?id=244507

I wanted to thank you for this great article, I definitely loved each and every little bit of it. I have bookmarked your web site to look at the latest stuff you post.


Blog Message http://sl-sms.by/user/Depoptova767/

purses too. I personally believe that any one of those totes

First Previous 1 2 3 4 5 Next Last 
Rating
Name
Email
Comment