Seach

how to display 15 recodes of data in the database in console application

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;//this name space is used to communicate with data base
namespace demo1
{
class Program
{
static void Main(string[] args)

{


SqlConnection c = new SqlConnection("Data Source=.;Initial Catalog=AdventureWorksDW;Integrated Security=True");// creating connection
try
{
string sql_cmd;
c.Open();//openig connection
Console.WriteLine("enter the cmd wishers to do:");//we can enter any sql cmd a we like in here
sql_cmd = Console.ReadLine();
SqlCommand cmd = new SqlCommand(sql_cmd,c);//creating sqlcmd

int count = 0;// counter flag
SqlDataReader d = cmd.ExecuteReader();//this step is used to read data from backend
Console.WriteLine("result of the qurry:");
while (d.Read())//fetching data
{
count = count + 1;// incrementing counter
Console.WriteLine( d[0]);
if (count >= 15)//when counter reach to 15 the execution of pgm stoped means fetch 15 recodes from the database
{
break;//exiting statement from loop
}

}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

finally
{
c.Close();//closing opened database connection


}
Console.ReadLine();
}
}




}




No comments: