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.

(292) Comments

Blog Message http://betaniceseo.pw/story.php?id=11729

Im thankful for the blog article.Really looking forward to read more.


Blog Message http://mygoldmountainsrock.com/2019/03/21/most-significant-online-gambling-web-page-in-indonesia/

It?s actually a great and helpful piece of info. I am satisfied that you shared this useful info with us. Please keep us informed like this. Thank you for sharing.


Blog Message https://1drv.ms/t/s!AlXmvXWGFuIdhuJ24H0kofw3h_cdGw

Thanks a lot for the blog article.Much thanks again. Great.


Blog Message http://denver-rtd.info/__media__/js/netsoltrademark.php?d=www.btobaby.it%2Findex.php%3Foption%3Dcom_k2%26view%3Ditemlist%26task%3Duser%26id%3D531494

Past Exhibition CARTApartment CART Apartment CART Blog


Blog Message http://house-best-speaker.com/2019/03/15/bagaimana-cara-mendaftar-akun-pb-garena-dan-juga-cara-verifikasinya-4/

It as actually a great and helpful piece of info. I am happy that you shared this useful information with us. Please stay us up to date like this. Thank you for sharing.


Blog Message http://david9464fw.blogs4funny.com/you-need-to-have-a-bed-1-water-and-guests-or-a-hostess-this-new-years-eve

you make running a blog look easy. The total look of your site is great, as well as the


Blog Message http://mp.result-nic.in/

In the great I always visit your blog everyday to read new topics.,:~-~


Blog Message http://nifnif.info/user/Batroamimiz927/

Right now it seems like WordPress is the best blogging platform available right now. (from what I ave read) Is that what you are using on your blog?


Blog Message http://jahandigital.ir/user/KenGeaspAccen617/

we came across a cool internet site that you just may well appreciate. Take a search in the event you want


Blog Message http://chixawothosa.mihanblog.com/post/comment/new/40/fromtype/postone/fid/15022086645989e29806212/atrty/1502208664/avrvy/0/key/e4aa166246d04ca8cd03c6ace966f27b/

I went over this web site and I think you have a lot of great info, saved to fav (:.


Blog Message http://smilepinkimovie.net/__media__/js/netsoltrademark.php?d=www.sicipiscine.it%2Findex.php%3Foption%3Dcom_k2%26view%3Ditemlist%26task%3Duser%26id%3D1049415

I truly appreciate this post.Really thank you!


Blog Message http://iluxgen.com/space-uid-680349.html

Well I sincerely liked studying it. This tip provided by you is very constructive for correct planning.


Blog Message http://ask.rescuedigitalmedia.com/index.php?qa=user&qa_1=closetkick70

You made some decent points there. I checked on the web for more information about the issue and found most individuals will go along with your views on this web site.


Blog Message http://www.uzaktanegitimrehberi.com/forum/uye-crediterror73.html

Say, you got a nice blog post.Really thank you! Really Cool.


Blog Message http://newvaweforbusiness.com/2019/02/26/totally-free-apk-download-for-pclaptop-and-mac/

you employ a fantastic weblog here! want to earn some invite posts on my website?

First Previous 1 2 3 4 5 6 7 8 9 10  ... Next Last 
Rating
Name
Email
Comment