Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / C#

A Very Simple Oracle Query Tool

Rate me:
Please Sign up or sign in to vote.
3.81/5 (12 votes)
11 Jul 2008CPOL3 min read 49.2K   1.1K   39   4
This is a very simple tool, to query an Oracle XE database

Introduction

While working on an Oracle XE project a while back, I developed a simple query tool to allow me to execute SQL statements and view the results in a DataGridView control. This functionality was already provided by Oracle APEX, a Web application supplied by Oracle XE, which allowed me to enter SQL statements to execute. However it did not list the table structure or table data, which would have helped, while constructing SQL statements. For example, if I wanted to retrieve specific columns from a table, I would need to know what the columns were called. Sometimes, I would forget what a column was called or how it was spelt, so I would have to go into the object browser and copy the column names. The SQL command interface does not list the tables or show you the table structure. It only allows you to enter SQL statements, if the statement was a SELECT query, it would show you the results of the query.

At times I would need column names from multiple tables, to construct an SQL JOIN statement. For this reason, I developed a simple query application. The application works in the following way. When logged in, a ListView control is populated when the "List Tables" button is clicked, showing the tables that belong to the user logged in. By selecting a table name from the ListView control, you can view the structure of the table. By clicking the appropriate buttons, you can switch between structure view or data view. The structure view will show the data types for each column. By switching to data view, you can see the data stored in each column. While constructing an SQL statement, you can easily view the table structure or the data. I have also included the ability to List VIEWS. You can see the structure of a VIEW as well as the data the VIEW will produce.

Below are a few screen shots of the working program. Figure 1.1 shows the login form. This form requires that you enter the data source name, user id and password.

Screenshot - img1.jpg

Figure 1.1

After logging in, you will be presented with the following form (Figure 1.2).

Screenshot - img2.jpg

Figure 1.2

Screenshot - img3.jpg

Figure 1.3

Screenshot - img4.jpg

Figure 1.4

The Code

I have separated the connection to the database code from the UI code. orcConnection class contains a Connect() method, which connects to the database. Listing 1.1 below shows the Connect() method.

Listing 1.1

C#
public static string Connect(string strDataSource, string strUserId, string strPassword)
{
string strConResult = "";

string orcConString = "Data Source=" + strDataSource + ";User Id=" + 
			strUserId + ";Password=" + strPassword + ";";
orcCon = new OracleConnection(orcConString);

try
{
orcCon.Open();
}
catch (OracleException orcEx)
{
strConResult = "An error occurred: " + orcEx.Message;
}

return strConResult;
} 

The Connect() method takes three arguments, a data source, user id and password all of type string. I have used a try/catch block to catch any errors. If any errors are produced, the error message is returned to the calling method, where it is displayed in a message box.

Update

Previously there was no way of executing an INSERT/UPDATE/DELETE statement, but thanks to No_Namer and dadox for their comments, the query tool now returns a message to the user if an INSERT/DELETE/UPDATE statement was successful. It also produces any errors that might occur when constructing the SQL statement.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHELP Pin
Member 86946022-Mar-12 22:50
Member 86946022-Mar-12 22:50 
GeneralInsert, Update and Delete Pin
No_Namer10-Aug-07 7:16
No_Namer10-Aug-07 7:16 
Generalinsert and update's Pin
dadox30-Jul-07 21:16
dadox30-Jul-07 21:16 
GeneralNice!! Pin
Alex Espinoza30-Jul-07 6:26
Alex Espinoza30-Jul-07 6:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.