Use of MARS for nested queries.

Ddd
Posted by Ddd under C# category on | Points: 40 | Views : 1555
//Use of MARS for nested queries.
//change the server name,login id and pwd as per the configuration of your system.
//database must be northwind.
//You can create your own database and display the output of 2 queries of your own tables.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ConsoleApplication20jan
{
class Class1
{
static void Main()
{
SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=1234;database=northwind;MultipleActiveResultsets=True");
SqlCommand cmd = new SqlCommand("select * from categories", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
string s = "";

while (dr.Read() == true)
{
SqlCommand cmd1 = new SqlCommand("select count(*) from products", cn);

object o = cmd1.ExecuteScalar();
Console.WriteLine(dr[0] + "---" + dr[1]);
s = o.ToString();

}
Console.WriteLine("total records of products table"+"--"+s);
cn.Close();
}

}
}

Comments or Responses

Login to post response