Oracle Database Viewer


Description

This is a GUI based application which connects to an Oracle database depending upon the DSN, username and password that you provide. It then shows the list of tables on the left hand side in the form of a treeview. Upon selecting a tablename from the treeview, the details of that particular table are displayed in the listview on the right hand side. Thus, using this application one can have a view of all the user defined tables in an Oracle database.

Souce code

//ViewDatabase.cs
// created on 10/6/2001 at 5:28 PM
using System ;
using System.ComponentModel ;
using System.Data ;
using System.Data.Odbc ;
using System.Drawing ;
using System.Windows.Forms ;
public class ViewDatabase:Form
{
private Panel leftPanel, rightPanel, topPanel, bottomPanel ;
private TreeView tableTree ;
private TextBox dsnText, uidText, pwdText ;
private Label tableLbl1, tableLbl2, tableLbl3, dsnLbl, uidLbl, pwdLbl ;
private Button connectBtn, disconnectBtn ;
private Splitter verticalSplitter, horizontalSplitter ;
private String Connection, query ;
private OdbcConnection myConnection ;
private OdbcCommand myCommand ;
private OdbcDataReader myReader ;
private ListView listData ;
private ErrorProvider ep ;
private PictureBox pictBox ;
public ViewDatabase()
{
this.Text = "Database Viewer" ;
this.Icon = new Icon("ViewDatabase.ico") ;
ep = new ErrorProvider() ;
ep.BlinkRate = 500 ;
ep.BlinkStyle = ErrorBlinkStyle.AlwaysBlink ;
//This font will be used uniformly throughout.
Font labelFont = new Font("Arial", 12, FontStyle.Underline) ;
Font labelFont1 = new Font("Arial", 12, FontStyle.Bold) ;
//Working with leftPanel.
leftPanel = new Panel() ; //------Created leftPanel.
tableLbl1 = new Label() ; //------Created tableLbl1.
tableLbl1.Text = "Tables:" ;
tableLbl1.Font = labelFont ;
tableLbl1.ForeColor = Color.FromArgb(255, 255, 255) ;
tableLbl1.Left = 10 ;
tableLbl1.TabIndex = 1 ;
tableTree = new TreeView() ;
tableTree.BackColor = Color.DarkGray ;
tableTree.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle ;
tableTree.Location = new Point(10, 30) ;
tableTree.TabIndex = 2 ;
tableTree.AfterSelect += new TreeViewEventHandler(tableTree_AfterSelect) ;
leftPanel.Controls.Add(tableTree) ;
leftPanel.Controls.Add(tableLbl1) ;
leftPanel.Dock = DockStyle.Left ;
leftPanel.TabIndex = 1 ;
leftPanel.Visible = true ;
leftPanel.Width = 230 ;
leftPanel.Height = this.ClientSize.Height ;
tableTree.Width = 210 ;
tableTree.Height = 530 ;
//Through with leftPanel.
//Working with topPanel.
topPanel = new Panel() ; //------Created topPanel.
topPanel.Size = new Size(534, 155) ;
dsnLbl = new Label() ; //------Created dsnLbl.
dsnLbl.Text = "DSN" ;
dsnLbl.Font = labelFont1 ;
dsnLbl.ForeColor = Color.White ;
dsnLbl.Location = new Point(5, 10) ;
dsnLbl.TabIndex = 1 ;
uidLbl = new Label() ; //------Created uidLbl.
uidLbl.Text = "Username" ;
uidLbl.Font = labelFont1 ;
uidLbl.ForeColor = Color.White ;
uidLbl.Location = new Point(5, 47) ;
uidLbl.TabIndex = 3 ;
pwdLbl = new Label() ; //------Created pwdLbl.
pwdLbl.Text = "Password" ;
pwdLbl.Font = labelFont1 ;
pwdLbl.ForeColor = Color.White ;
pwdLbl.Location = new Point(5, 85) ;
pwdLbl.TabIndex = 5 ;
dsnText = new TextBox() ; //------Created dsnText.
dsnText.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle ;
dsnText.Location = new Point(dsnLbl.Width + 5, 10) ;
dsnText.GotFocus += new EventHandler(dsn_Focus) ;
dsnText.Validating += new CancelEventHandler(dsnValidate) ;
dsnText.Width = 200 ;
dsnText.TabIndex = 2 ;
uidText = new TextBox() ; //------Created uidText.
uidText.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle ;
uidText.Location = new Point(uidLbl.Width + 5, 47) ;
uidText.GotFocus += new EventHandler(uid_Focus) ;
uidText.Validating += new CancelEventHandler(uidValidate) ;
uidText.Width = 200 ;
uidText.TabIndex = 4 ;
pwdText = new TextBox() ; //------Created pwdText.
pwdText.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle ;
pwdText.PasswordChar = '*' ;
pwdText.Location = new Point(pwdLbl.Width + 5, 85) ;
pwdText.GotFocus += new EventHandler(pwd_Focus) ;
pwdText.Validating += new CancelEventHandler(pwdValidate) ;
pwdText.Width = 200 ;
pwdText.TabIndex = 6 ;
connectBtn = new Button() ;
connectBtn.Text = "Connect" ;
connectBtn.FlatStyle = FlatStyle.Flat ;
connectBtn.BackColor = Color.Khaki ;
connectBtn.Location = new Point(100, 120) ;
connectBtn.Width = 100 ;
connectBtn.TabIndex = 9 ;
connectBtn.MouseHover += new EventHandler(connectBtn_MouseHover) ;
connectBtn.MouseLeave += new EventHandler(connectBtn_MouseLeave) ;
connectBtn.Click += new EventHandler(connectBtn_Click) ;
disconnectBtn = new Button() ;
disconnectBtn.Text = "Disconnect" ;
disconnectBtn.FlatStyle = FlatStyle.Flat ;
disconnectBtn.BackColor = Color.Khaki ;
disconnectBtn.Location = new Point(215, 120) ;
disconnectBtn.Width = 100 ;
disconnectBtn.Enabled = false ;
disconnectBtn.TabIndex = 10 ;
disconnectBtn.MouseHover += new EventHandler(disconnectBtn_MouseHover) ;
disconnectBtn.MouseLeave += new EventHandler(disconnectBtn_MouseLeave) ;
disconnectBtn.Click += new EventHandler(disconnectBtn_Click) ;
pictBox = new PictureBox() ;
pictBox.Size = new Size(100, 100) ;
pictBox.Location = new Point(400, 10) ;
pictBox.Image = Image.FromFile("logo.jpg") ;
pictBox.TabIndex = 11 ;
topPanel.Controls.Add(pictBox) ;
topPanel.Controls.Add(disconnectBtn) ;
topPanel.Controls.Add(connectBtn) ;
topPanel.Controls.Add(pwdText) ;
topPanel.Controls.Add(pwdLbl) ;
topPanel.Controls.Add(uidText) ;
topPanel.Controls.Add(uidLbl);
topPanel.Controls.Add(dsnText) ;
topPanel.Controls.Add(dsnLbl) ;
topPanel.Dock = DockStyle.Top ;
topPanel.TabIndex = 1 ;
topPanel.Visible = true ;
//Through with topPanel.
tableLbl2 = new Label() ; //------Created tableLbl2.
tableLbl2.Text = "Table Details:" ;
tableLbl2.Left = 10 ;
tableLbl2.Font = labelFont ;
tableLbl2.ForeColor = Color.FromArgb(255, 255, 255) ;
tableLbl2.TabIndex = 1 ;
tableLbl3 = new Label() ; //------Created tableLbl2.
tableLbl3.Font = labelFont1 ;
tableLbl3.ForeColor = Color.FromArgb(255, 255, 255) ;
tableLbl3.Location = new Point((tableLbl2.Left + tableLbl2.Size.Width) + 10, 0) ;
tableLbl3.BackColor = Color.Yellow ;
tableLbl3.ForeColor = Color.Green ;
tableLbl3.TextAlign = ContentAlignment.MiddleCenter ;
tableLbl3.TabIndex = 2 ;
listData = new ListView() ;
listData.View = View.Details ;
listData.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle ;
listData.GridLines = true ;
listData.FullRowSelect = true ;
listData.MultiSelect = false ;
listData.Width = 540 ;
listData.Height = 373 ;
listData.Location = new Point(10, 30) ;
listData.TabIndex = 3 ;
bottomPanel = new Panel() ; //------Created bottomPanel.
bottomPanel.Size = new Size(534, 415) ;
tableLbl2.Width = 150 ;
tableLbl3.Width = 430 ;
bottomPanel.Controls.Add(listData) ;
bottomPanel.Controls.Add(tableLbl3) ;
bottomPanel.Controls.Add(tableLbl2) ;
bottomPanel.Dock = DockStyle.Bottom ;
bottomPanel.TabIndex = 3 ;
bottomPanel.Visible = true ;
//Through with bottomPanel.
horizontalSplitter = new Splitter() ;//-----Created horizontalSplitter.
horizontalSplitter.Dock = DockStyle.Top ;
horizontalSplitter.TabIndex = 2 ;
horizontalSplitter.Height = 3 ;
horizontalSplitter.BackColor = Color.Red ;
horizontalSplitter.Enabled = false ;
horizontalSplitter.TabStop = false ;
rightPanel = new Panel() ; //------Created rightPanel.
rightPanel.Size = new Size(559, this.ClientSize.Height) ;
rightPanel.Controls.Add(bottomPanel) ;
rightPanel.Controls.Add(horizontalSplitter) ;
rightPanel.Controls.Add(topPanel) ;
rightPanel.Dock = DockStyle.Right ;
rightPanel.TabIndex = 3 ;
rightPanel.Visible = true ;
verticalSplitter = new Splitter() ;//-------Created verticalSplitter.
verticalSplitter.Size = new Size(3, ClientSize.Height) ;
verticalSplitter.BackColor = Color.Red ;
verticalSplitter.TabIndex = 2 ;
verticalSplitter.TabStop = false ;
verticalSplitter.Enabled = false ;
this.Controls.Add(rightPanel) ;
this.Controls.Add(verticalSplitter) ;
this.Controls.Add(leftPanel) ;
this.Size = new Size(800, 600) ;
this.MaximizeBox = false ;
this.BackColor = Color.DarkGray ;
this.Location = new Point(0, 0) ;
this.AcceptButton = connectBtn ;
dsnText.Focus() ;
}
private void dsn_Focus(object sender, EventArgs e)
{
dsnText.SelectAll() ;
}
private void uid_Focus(object sender, EventArgs e)
{
uidText.SelectAll() ;
}
private void pwd_Focus(object sender, EventArgs e)
{
pwdText.SelectAll() ;
}
private void dsnValidate(object sender, CancelEventArgs e)
{
if (dsnText.Text == "")
{
ep.SetError(dsnText, "Please enter a DSN.") ;
e.Cancel = true ;
dsnText.Focus() ;
}
else
{
ep.SetError(dsnText, "") ;
}
}
private void uidValidate(object sender, CancelEventArgs e)
{
if (uidText.Text == "")
{
ep.SetError(uidText, "Please enter a Username.") ;
e.Cancel = true ;
uidText.Focus() ;
}
else
{
ep.SetError(uidText, "") ;
}
}
private void pwdValidate(object sender, CancelEventArgs e)
{
if (pwdText.Text == "")
{
ep.SetError(pwdText, "Please enter a Password.") ;
e.Cancel = true ;
pwdText.Focus() ;
}
else
{
ep.SetError(pwdText, "") ;
}
}
private void connectBtn_MouseHover(object sender, EventArgs e)
{
connectBtn.Text = "C o n n e c t" ;
}
private void connectBtn_MouseLeave(object sender, EventArgs e)
{
connectBtn.Text = "Connect" ;
}
private void disconnectBtn_MouseHover(object sender, EventArgs e)
{
disconnectBtn.Text = "D i s c o n n e c t" ;
}
private void disconnectBtn_MouseLeave(object sender, EventArgs e)
{
disconnectBtn.Text = "Disconnect" ;
}
private void connectBtn_Click(object sender, EventArgs e)
{
try
{
tableTree.Nodes.Clear() ;
listData.Columns.Clear() ;
listData.Items.Clear() ;
Connection = "Provider=MSDAORA;DSN=" + dsnText.Text + ";UID=" + uidText.Text + ";PWD=" + pwdText.Text ;
myConnection = new OdbcConnection(Connection) ;
query = "select * from tab" ;
myCommand = new OdbcCommand(query, myConnection) ;
myCommand.Connection.Open() ;
myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection) ;
while(myReader.Read())
{
tableTree.Nodes.Add(myReader.GetString(0)) ;
}
myReader.Close() ;
myCommand.Connection.Close() ;
myConnection.Close() ;
connectBtn.Enabled = false ;
disconnectBtn.Enabled = true ;
dsnText.Enabled = false ;
uidText.Enabled = false ;
pwdText.Enabled = false ;
}
catch(Exception ex)
{
if (ex.Message.IndexOf("Data source") != -1)
{
ep.SetError(dsnText, "Please enter a valid DSN!") ;
dsnText.Focus() ;
}
else
{
if (ex.Message.IndexOf("invalid username/password") != -1)
{
ep.SetError(uidText, "Please enter a valid Username/Password!") ;
uidText.Focus() ;
}
}
}
}
private void disconnectBtn_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to disconnect?", "Disconnect",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
if (!myReader.IsClosed)
myReader.Close() ;
myConnection.Close() ;
tableTree.Nodes.Clear() ;
listData.Columns.Clear() ;
listData.Items.Clear() ;
connectBtn.Enabled = true ;
disconnectBtn.Enabled = false ;
tableLbl3.Text = "" ;
dsnText.Enabled = true ;
uidText.Enabled = true ;
pwdText.Enabled = true ;
dsnText.Text = "" ;
uidText.Text = "" ;
pwdText.Text = "" ;
dsnText.Focus() ;
}
}
private void tableTree_AfterSelect(object sender, TreeViewEventArgs e)
{
int dispInd = 0 ;
try
{
listData.Columns.Clear() ;
listData.Items.Clear() ;
Connection = "Provider=MSDAORA;DSN=" + dsnText.Text + ";UID=" +uidText.Text + ;
PWD=" + pwdText.Text ;
myConnection = new OdbcConnection(Connection) ;
query = "select * from " + e.Node.Text ;
myCommand = new OdbcCommand(query, myConnection) ;
myCommand.Connection.Open() ;
myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection) ;
for (int i = 0; i < myReader.FieldCount; i++)
{
listData.Columns.Add(myReader.GetName(i), 100,HorizontalAlignment.Center) ;
}
while(myReader.Read())
{
if (!myReader.IsDBNull(0))
listData.Items.Add(myReader.GetValue(0).ToString());
for (int i = 1; i < myReader.FieldCount; i++)
{
if (!myReader.IsDBNull(i))
listData.Items[dispInd].SubItems.Add(myReader.GetValue(i).ToString());
}
dispInd++ ;
}
myReader.Close() ;
myCommand.Connection.Close() ;
myConnection.Close() ;
tableLbl3.Text = e.Node.Text ;
}
catch(Exception ex)
{
MessageBox.Show("There was some error connecting to the database.", "DB Error", MessageBoxButtons.OK, MessageBoxIcon.Error) ;
}
}
public static void Main()
{
Application.Run(new ViewDatabase()) ;
}
}


Similar Articles