Execute multiple SQL Queries in one Statement using C#

In the example below I have explained how to execute multiple sql queries as a single statement using the c# code.

Download Sql table and data given below.

Download Sql table schema and data

Aspx Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Execute multiple SQL Queries in one Statement using C# </title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h3>Execute multiple SQL Queries in one Statement using C# </h3>
            <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Customer Name" ItemStyle-Width="150px" />
                    <asp:BoundField DataField="Age" HeaderText="Age" ItemStyle-Width="100px" />
                    <asp:BoundField DataField="Gender" HeaderText="Gender" ItemStyle-Width="100px" />
                </Columns>
            </asp:GridView>
            <br />
            <asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField DataField="StudName" HeaderText="Student Name" ItemStyle-Width="150px" />
                    <asp:BoundField DataField="StudCity" HeaderText="City" ItemStyle-Width="100px" />
                    <asp:BoundField DataField="StudTel" HeaderText="Contact Number" ItemStyle-Width="100px" />
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

 

Cs Code for executing multiple SQL Queries

I have used dataset to have multiple tables inside it and displayed the final output using Grid control in C#.

protected void Page_Load(object sender, EventArgs e)
       {
           if (!this.IsPostBack)
           {
               string constr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
               string query = "SELECT TOP 10 Name, Age, Gender FROM Customer;";
               query += "SELECT TOP 10 StudName, StudCity, StudTel FROM Students";

               using (SqlConnection con = new SqlConnection(constr))
               {
                   using (SqlCommand cmd = new SqlCommand(query))
                   {
                       using (SqlDataAdapter sda = new SqlDataAdapter())
                       {
                           cmd.Connection = con;
                           sda.SelectCommand = cmd;
                           using (DataSet ds = new DataSet())
                           {
                               sda.Fill(ds);
                               gvCustomers.DataSource = ds.Tables[0];
                               gvCustomers.DataBind();
                               gvStudents.DataSource = ds.Tables[1];
                               gvStudents.DataBind();
                           }
                       }
                   }
               }
           }
       }

 

Output

Output for Executing multiple SQL Queries

Demo Code

Download Demo