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:
using System.Data;
using System.Data.OracleClient;
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();
}
}
In the above code:
Now compile and run the application.
Enter any TNS Service Name in the DataSourceTextBox, user name in the UserIdTextBox and password in the PasswordTextBox and click on the TestConnButton.
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.
15 March 2022
17 February 2022
09 December 2019