Execute your Sql query : Database Utilities « Database ADO.net « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Database ADO.net » Database Utilities 




Execute your Sql query

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;

public class Queries : System.Windows.Forms.Form {
    private System.Windows.Forms.TextBox txtResult;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Button cmdExecute;
    private System.Windows.Forms.TextBox txtSql;
    private System.ComponentModel.Container components = null;

    public Queries() {
        InitializeComponent();
    }
    private void InitializeComponent() {
        this.txtSql = new System.Windows.Forms.TextBox();
        this.txtResult = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.cmdExecute = new System.Windows.Forms.Button();
        this.SuspendLayout();

        this.txtSql.Location = new System.Drawing.Point(032);
        this.txtSql.Multiline = true;
        this.txtSql.Name = "txtSql";
        this.txtSql.Size = new System.Drawing.Size(40072);
        this.txtSql.TabIndex = 0;
        this.txtSql.Text = "";

        this.txtResult.Location = new System.Drawing.Point(0184);
        this.txtResult.Multiline = true;
        this.txtResult.Name = "txtResult";
        this.txtResult.Size = new System.Drawing.Size(40088);
        this.txtResult.TabIndex = 1;
        this.txtResult.Text = "";

        this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif"8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        this.label1.Location = new System.Drawing.Point(88);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(38416);
        this.label1.TabIndex = 2;
        this.label1.Text = "Type a SQL statement in the text box.";
        this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

        this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif"8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        this.label2.Location = new System.Drawing.Point(0160);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(39216);
        this.label2.TabIndex = 3;
        this.label2.Text = "Execution Result";
        this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

        this.cmdExecute.Location = new System.Drawing.Point(152112);
        this.cmdExecute.Name = "cmdExecute";
        this.cmdExecute.Size = new System.Drawing.Size(10432);
        this.cmdExecute.TabIndex = 4;
        this.cmdExecute.Text = "Execute Command";
        this.cmdExecute.Click += new System.EventHandler(this.cmdExecute_Click);

        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(400275);
        this.Controls.Add(this.cmdExecute);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.txtResult);
        this.Controls.Add(this.txtSql);
        this.Name = "Queries";
        this.Text = "Tables and Relationships";
        this.ResumeLayout(false);
   }
   private void cmdExecute_Click(object sender, System.EventArgs e) {
        try{
            SqlConnection conn = new SqlConnection(@"server=(local)\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI")

            conn.Open();
            string strSQL=txtSql.Text;
            SqlCommand cmd= new SqlCommand(strSQL, conn);

            cmd.ExecuteReader();
            conn.Close();
            txtResult.Text = "SQL executed successfully.";
         catch (System.Data.SqlClient.SqlException ex) {
            txtResult.Text =
               "There was an error in executing the SQL. " +
               "Error Message:" + ex.Message; 
         }

   }
   static void Main() {
      Application.Run(new Queries());
   }
}


           
       














Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.