Thursday, 27 June 2013

How to execute multiple SQL statements using a SqlCommand object

How to execute multiple SQL statements using a SqlCommand object


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection newConnection = new 
    SqlConnection(@"Data Source=R4R-3EFD13BB468\SQLEXPRESS;
    initial catalog=newDatabase;integrated security=true;");
    protected void Page_Load(object sender, EventArgs e)
    {
      SqlCommand cmd = new 
      SqlCommand("insert into emp_tbl (nme,age,emp_id) values ('atul', '24','1234');" +
      "select nme,age from emp_tbl where emp_id=1234 "+
      "update emp_tbl set nme='Anjali' where emp_id=1234" +
      "select nme,age from emp_tbl where emp_id=1234 ;",newConnection);
      newConnection.Open();
      SqlDataReader dr = cmd.ExecuteReader();
      if (dr.Read())
      {
          GridView1.DataSource = dr;
          GridView1.DataBind();
      }
      newConnection.Close();
    }
}
Out Put:

No comments: