using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button cmdExecute;
private System.Windows.Forms.TextBox txtSql;
private System.Windows.Forms.ListView lvwResult;
private System.ComponentModel.Container components = null;
public Form1() {
InitializeComponent();
}
private void InitializeComponent() {
this.label1 = new System.Windows.Forms.Label();
this.cmdExecute = new System.Windows.Forms.Button();
this.txtSql = new System.Windows.Forms.TextBox();
this.lvwResult = new System.Windows.Forms.ListView();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(296, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Enter a SQL query or statement and click Execute.";
this.cmdExecute.Location = new System.Drawing.Point(227, 224);
this.cmdExecute.Name = "cmdExecute";
this.cmdExecute.TabIndex = 1;
this.cmdExecute.Text = "Execute";
this.cmdExecute.Click += new System.EventHandler(this.cmdExecute_Click);
this.txtSql.Location = new System.Drawing.Point(0, 16);
this.txtSql.Multiline = true;
this.txtSql.Name = "txtSql";
this.txtSql.Size = new System.Drawing.Size(528, 200);
this.txtSql.TabIndex = 2;
this.txtSql.Text = "";
this.lvwResult.GridLines = true;
this.lvwResult.Location = new System.Drawing.Point(0, 256);
this.lvwResult.Name = "lvwResult";
this.lvwResult.Size = new System.Drawing.Size(528, 200);
this.lvwResult.TabIndex = 3;
this.lvwResult.View = System.Windows.Forms.View.Details;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(528, 452);
this.Controls.Add(this.lvwResult);
this.Controls.Add(this.txtSql);
this.Controls.Add(this.cmdExecute);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Query Processor";
this.ResumeLayout(false);
}
static void Main() {
Application.Run(new Form1());
}
private void cmdExecute_Click(object sender, System.EventArgs e) {
SqlConnection conn = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI");
try {
lvwResult.Columns.Clear() ;
lvwResult.Items.Clear();
conn.Open();
txtSql.Text ="select * from Employee";
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = txtSql.Text;
SqlDataReader dr = cmd.ExecuteReader();
for (int i = 0; i< dr.FieldCount; i++) {
ColumnHeader ch = new ColumnHeader();
ch.Text=dr.GetName(i);
lvwResult.Columns.Add(ch);
}
ListViewItem itmX;
while (dr.Read()) {
itmX=new ListViewItem();
itmX.Text= dr.GetValue(0).ToString();
for (int i=1 ; i< dr.FieldCount; i++) {
itmX.SubItems.Add(dr.GetValue(i).ToString());
}
lvwResult.Items.Add(itmX);
}
dr.Close();
} catch ( System.Data.SqlClient.SqlException ex) {
Console.WriteLine("There was an error in executing the SQL." +
"\nError Message:" + ex.Message, "SQL");
} finally {
conn.Close();
}
}
}