Query Management Studio in .NET

Introduction

Query Management Studio can be run in a browser and it allows the user to manage the database like insert, select, update and delete operations. QMS is a simple application to understand. You can use this application as GUI for your database. In making this application, I use SQL Server 2005, .NET Framework 3.5 and C#. This article is meant for all kinds of beginner users who want to get started with SQL Server. I have included a small feather of SQL Server like insert, update and delete in QMS.

Background

The basic idea behind making OQMS is that some time ago, I used a hosting panel where I created a table on a server but the server response was very slow and sometimes, the server was getting stuck and not responding. Then I made this web application and using this application, you need to use your SQL Server that is intalled on your machine and it does DDL and DML operations.

Using the Code

For login form: Namespace: 
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Data.Sql;  
  11. using System.Data.SqlClient;  
  12. using System.IO;  
  13. using System.Drawing;  
And the C# code is:
  1. public partial class Entrance : System.Web.UI.Page  
  2. {  
  3.     SqlConnection con = new SqlConnection();  
  4.     protected void Page_Load(object sender, EventArgs e)  
  5.     {  
  6.     }  
  7.     protected void btncon_Click(object sender, ImageClickEventArgs e)  
  8.     {  
  9.         if (txtdbase.Text == "" || txtserv.Text == "" || txtuid.Text == "" || txtpass.Text == "")  
  10.         {  
  11.             Page.RegisterStartupScript("UserMsg",  
  12.             "alert('All field should be filled');if(alert){ window.location='Entrance.aspx';}");  
  13.         }  
  14.         else  
  15.         {  
  16.             Session["dbase"] = txtdbase.Text;  
  17.             Session["sname"] = txtserv.Text;  
  18.             Session["uid"] = txtuid.Text;  
  19.             Session["pass"] = txtpass.Text;  
  20.             Response.Redirect("~/Default.aspx"false);  
  21.         }  
  22.     }  
  23. }   
And the very next form:
  1. public partial class _Default : System.Web.UI.Page  
  2. {  
  3.     SqlConnection con = new SqlConnection();  
  4.     protected void Page_Load(object sender, EventArgs e)  
  5.     {  
  6.         string server = Session["sname"].ToString();  
  7.         string dname = Session["dbase"].ToString();  
  8.         string uid = Session["uid"].ToString();  
  9.         string pass = Session["pass"].ToString();  
  10.        con.ConnectionString = "Data Source="+server +";  
  11.        Initial Catalog="+ dname  +"; User ID=" + uid +"; Password=" + pass ;  
  12.         if(!IsPostBack)  
  13.         getdbase();  
  14.     }  
  15.     protected void btnreset_Click(object sender, EventArgs e)  
  16.     {  
  17.         querybox.Text = "";  
  18.         ListBox2.Items.Clear();  
  19.         GridView1.Dispose();  
  20.     }  
  21.     protected void btnexcu_Click(object sender, EventArgs e)  
  22.     {  
  23.         functions();  
  24.     }  
  25.     void functions()  
  26.     {  
  27.         try  
  28.         {  
  29.             if (querybox.Text != "")  
  30.             {  
  31.                 if (hidSelectedText.Value == "")  
  32.                 {  
  33.                     hidSelectedText.Value = querybox.Text;  
  34.                 }  
  35.                 if (hidSelectedText.Value != "")  
  36.                 {  
  37.                     con.Open();  
  38.                     SqlCommand cmd = new SqlCommand("use " +  
  39.                     ddl.SelectedItem.ToString() + " " + hidSelectedText.Value, con);  
  40.                    // SqlCommand cmd = new SqlCommand(hidSelectedText.Value, con);  
  41.                     SqlDataAdapter da = new SqlDataAdapter(cmd);  
  42.                     DataTable dt = new DataTable();  
  43.                     da.Fill(dt);  
  44.                     GridView1.DataSource = dt;  
  45.                     GridView1.DataBind();  
  46.                     con.Close();  
  47.                     lblmes.Visible = true;  
  48.                     war.Visible = false;  
  49.                     ok.Visible = true;  
  50.                     lblmes.ForeColor = Color.Green;  
  51.                     lblmes.Text = "Query executed successfully";  
  52.                 }  
  53.                 else  
  54.                 {  
  55.                     lblmes.Visible = true;  
  56.                     ok.Visible = false;  
  57.                     war.Visible = true;  
  58.                     lblmes.ForeColor = Color.DarkGoldenrod;  
  59.                     lblmes.Text = "Query completed with error";  
  60.                 }  
  61.             }  
  62.             else  
  63.             {  
  64.                 querybox.Text = "Write some query or select some text...";  
  65.             }  
  66.         }  
  67.         catch (Exception  ex)  
  68.         {  
  69.             querybox.Text = ex.Message;  
  70.             lblmes.Visible = true;  
  71.             ok.Visible = false;  
  72.             war.Visible = true;  
  73.             lblmes.ForeColor = Color.Red;  
  74.             lblmes.Text = "Query completed with error";  
  75.             GridView1.Dispose();  
  76.         }  
  77.       }  
  78.     protected void ddl_SelectedIndexChanged(object sender, EventArgs e)  
  79.     {  
  80.         try  
  81.         {  
  82.             ListBox2.Items.Clear();  
  83.             con.Open();  
  84.             string qry = ddl.SelectedItem.ToString();  
  85.             SqlCommand cmd = new SqlCommand("USE "+qry  +   
  86.             " select name from sysobjects where type='U' ", con);  
  87.             SqlDataReader dr = cmd.ExecuteReader();  
  88.             ListBox1.Items.Clear();  
  89.             while (dr.Read())  
  90.             {  
  91.                 ListBox1.Items.Add(dr["name"].ToString());  
  92.             }  
  93.             con.Close();  
  94.         }  
  95.         catch(Exception ex)  
  96.         {  
  97.             querybox.Text = ex.Message;  
  98.         }  
  99.     }  
  100.     protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)  
  101.     {  
  102.         try  
  103.         {  
  104.             con.Open();  
  105.             string qry =  ListBox1 .SelectedItem.ToString();  
  106.             SqlCommand cmd = new SqlCommand("use " + ddl.SelectedItem.ToString() + "  
  107.             select Column_Name from INFORMATION_SCHEMA.COLUMNS _  
  108.             where TABLE_NAME = '" + qry + "'", con);  
  109.             SqlDataReader dr = cmd.ExecuteReader();  
  110.             ListBox2.Items.Clear();  
  111.             while (dr.Read())  
  112.             {  
  113.              ListBox2.Items.Add(dr["Column_Name"].ToString());  
  114.             }  
  115.             con.Close();  
  116.         }  
  117.         catch (Exception ex)  
  118.         {  
  119.             querybox.Text = ex.Message;  
  120.         }  
  121.     }  
  122.     void getdbase()  
  123.     {  
  124.         try  
  125.         {  
  126.              con.Open();  
  127.              SqlCommand cmd = new SqlCommand("SELECT name FROM master.dbo.sysdatabases", con);  
  128.              SqlDataReader dr = cmd.ExecuteReader();  
  129.              ddl.Items.Clear();  
  130.              while (dr.Read())  
  131.               {  
  132.                 ddl.Items.Add(dr["name"].ToString());  
  133.               }  
  134.               con.Close();  
  135.               lblststus.ForeColor = Color.Green;  
  136.               lblststus.Text = "Connected";  
  137.         }  
  138.         catch (Exception ex)  
  139.         {  
  140.             querybox.Text = ex.Message;  
  141.         }  
  142.     }  
  143. }  
Soon I will have a new version available!
 


Similar Articles