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

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Database ADO.net » Database UtilitiesScreenshots 
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
w__w__w___._ja__va__2__s_.c__o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.