Skip to main content

SQL Management Application.

SQL Management Application

We will List All SQL Server Instances available in Network into Application.
Then We will connect to Server using Windows Authentication or SQL Server Authentication.
After successfull authentication ,
We will list all databases in Instance.
After Database, we will List All Tables in Database,
After Database , we will list All Columns in Selected table.
We will place a Create button next to Database, Table list box also.
After selecting a Column, we provided a Filter (operator) and parameters in box.
Which will fetch data and display in DataGridView.

First we Designed a Following Screen.

In Servers List we have to Load All List of Servers and Connect. We have Options for Username and Password and also a Checkbox for Integrated Security (Windows Authentication).
Lets See the Code in Form Load.
//********************************************************************************
Namespace to Include: 


using System.Data.Sql;
using System.Data.SqlClient;

Now Form_Load Event Code




private void Form1_Load(object sender, EventArgs e)
        {
            panel1.Visible = false;
            groupBox1.Visible = false;
            label9.Visible = true;
            label9.TextAlign = ContentAlignment.MiddleCenter;
            SqlDataSourceEnumerator SerInstances = SqlDataSourceEnumerator.Instance;
            DataTable SerNames = SerInstances.GetDataSources();
            for (int i = 0; i <= SerNames.Rows.Count - 1; i++)
            {
                comboBox1.Items.Add(SerNames.Rows[i][0].ToString()+"\\"+SerNames.Rows[i][1].ToString());
            }
        }
//********************************************************************************
Description of Above Code: We have placed rest design in Panel and Groupbox so we set that invisible on Form Load. Please Make a Conncetion First.. is Label9 and we set is Visible and aligned. Now main task is to List Sql Servers on Network. For this we used SqlDataSourceEnumerator class. In Next Line we take a DataTable and stored ServerInstances into that. Rest is Simple, We added all items into Listbox. Column 0 contains Machine name and Column 1 contains Instance Name. So we combined that at the Time of Adding to ComboBox Control. Now Click the Connect Button:
//********************************************************************************
private void button1_Click(object sender, EventArgs e)       
{           
if (checkBox1.Checked == true && textBox1.Text.Trim().Length > 0)           
{               
MessageBox.Show("Please Uncheck Integrated Security or Clear UserName!", this.Text + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);          
}           
else if (checkBox1.Checked == false && textBox1.Text.Trim().Length == 0)           {               
MessageBox.Show("Please Check Integrated Security or Enter UserName!", this.Text + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);           
}
else if (textBox1.Text.Trim().Length > 0 && textBox2.Text.Trim().Length == 0)           {               
errorProvider1.SetError(textBox2, "Please enter password for Username = " + textBox1.Text + "!");           
}
else if (comboBox1.Text.Trim().Length == 0)           
{               
MessageBox.Show("Please select Server from List. If You can not see any Instance in Servers Combobox, Contact your Network Administrator!", this.Text + " - Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);           
}           
else           
{               
errorProvider1.Clear();               
if (MessageBox.Show("Connection Successfull!, You are Now Connected to Server: " + comboBox1.Text.ToString() + "!", this.Text + " - Alert", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)               {                   comboBox2.Items.Clear();                   
if (checkBox1.Checked == true)                   
{                       
ConStr = "Data Source = " + comboBox1.Text.ToString() + ";Integrated Security = true;";                   
}                   
else                   
{                       
ConStr = "Data Source = " + comboBox1.Text.ToString() + ";UID=" + textBox1.Text + ";pwd=" + textBox2.Text + ";";                   
}                   
SqlConnection Conexion = new SqlConnection(ConStr);                   Conexion.Open();                   
label9.Visible = false;                   
panel2.Visible = false;                   
button2.Visible = true;                   
panel1.Visible = true;                   
groupBox1.Visible = true;                   
DataTable tblDatabases = Conexion.GetSchema("Databases");
 
for (int i = 0; i <= tblDatabases.Rows.Count - 1; i++)                   
{                       
comboBox2.Items.Add(tblDatabases.Rows[i][0].ToString());                   
}                   
Conexion.Close();               
}           
}       
}
//*******************************************************************************
Description of Code: We Put some Validation and Message box to make some user friendly environment. After getting Proper Username and password, we make a Connection String and Used Username and password in Connection String. We build a connection, Make Servers List, Username and password and Connect button invisible and Disconnect button Visible. Then First we loaded all Databases of Selected Instance using Connection.GetSchema() method. How it look after connecting.

Comments

Popular posts from this blog

Creating package in Oracle Database using Toad For Oracle

What are Packages in Oracle Database A package is  a group   of procedures, functions,  variables   and  SQL statements   created as a single unit. It is used to store together related objects. A package has two parts, Package  Specification  and Package Body.

Resolving 'Setup Account Privileges' error while installing SQL Server

A new installation of Microsoft SQL Server 2012 or Microsoft SQL Server 2008 R2 fails You see the following error message when you try to install a new instance of SQL Server 2012 or SQL Server 2008 R2: Rule "Setup account privileges" failed.

Creating Oracle stored Procedures using TOAD for Oracle

In a database management system, a  stored procedure  is a set of Structured Query Language (SQL) statements with an assigned name that's stored in the database in compiled form so that it can be shared by a number of programs. The use of  stored procedures  can be helpful in controlling  access to data, preserving  data integrity  and  improving  productivity.