SQL Executer

Introduction

Installing Oracle or SQL Server just to practice SQL Queries is not a good idea. They occupy a lot of space and also slows down the normal processing of the pc. The better option is to use MS Access. It comes with MS Office package and normally it is easily available on every pc.

Designing User Inetrface

Sql.gif

  1. TextBox in Top Center for writing Sql queries
  2. ListBox on Left for List of Tables
  3. DataGridView for showing data from a particular table

Using the Code

Add following in the project

  1. using System.Data.OleDb;  
  2. using System.IO;  
Define Variables for use in the code

 

  1. private OleDbConnection mycon;  
  2. private DataSet ds;  
  3. private OleDbDataAdapter da;  
  4. private string sqlcmd = "SELECT * FROM Class";  
  5. protected void Page_Load(object sender, EventArgs e)  
On Load, fill the list box from the tables present in the database.
  1. void table()  
  2. {  
  3. listBox1.Items.Clear();  
  4. textBox1.Text = "";  
  5. mycon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Directory.GetCurrentDirectory() + "\\zipcodes.mdb;Persist Security Info=True");  
  6. mycon.Open();  
  7. DataTable tables = mycon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { nullnullnull"TABLE" });  
  8. foreach (DataRow row in tables.Rows)  
  9. listBox1.Items.Add(row[2]);  
  10. mycon.Close();  
  11. mycon.Dispose();  
  12. }  
The above code gets the name of tables from database and add them to ListBox

Now we need to make SelectedIndexChange Event for the ListBox
  1. private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         string tname = listBox1.SelectedItem.ToString();  
  6.         sqlcmd = "select * from " + tname;  
  7.         execute();  
  8.     }  
  9.     catch (Exception ex)  
  10.     {  
  11.         Messagebox.Show(ex.Message);  
  12.     }  
  13. }  
Execute() function fills the DataGridView with the data extracted from the database as result of sqlcmd.
  1. void execute()  
  2. {  
  3. try  
  4. {  
  5. mycon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Directory.GetCurrentDirectory() + "\\zipcodes.mdb;Persist Security Info=True");  
  6. ds = new DataSet();  
  7. ds.Clear();  
  8. mycon.Open();  
  9. da = new OleDbDataAdapter(sqlcmd, mycon);  
  10. da.Fill(ds, "mydata");  
  11.    
  12. dataGridView1.DataSource = ds.Tables["mydata"];  
  13. label3.Text = "Rows=" + ds.Tables["mydata"].Rows.Count.ToString();  
  14. label4.Text = "Columns=" + ds.Tables["mydata"].Columns.Count.ToString();  
  15. mycon.Close();  
  16. mycon.Dispose();  
  17. }  
  18. catch (OleDbException ole_execp)  
  19. {  
  20. MessageBox.Show("Error in Command Execution");  
  21. }  
  22. catch (Exception ex)  
  23. {  
  24. MessageBox.Show(ex.Message);  
  25. }   
  26. }  
Finally write the Run Button Click Event, to execute user sql query

  1. private void button1_Click_1(object sender, EventArgs e)  
  2. {  
  3.     if ((textBox1.Text != "") && (textBox1.Text != "Write Command") && (textBox1.Text != "Some Mistake in Command"))  
  4.     {  
  5.         sqlcmd = textBox1.Text;  
  6.         execute();  
  7.         table();  
  8.     }  
  9.     else  
  10.     {  
  11.         textBox1.Text = "Write Command";  
  12.     }  
  13. }  
The article is written for beginners, the methods to communicate with database are kept simple, complex and improve code version for dealing with database can be viewed here

http://www.c-sharpcorner.com/uploadfile/2a62a4/how-to-easily-query-a-database/

 

Up Next
    Ebook Download
    View all
    Learn
    View all