Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

SqldataReader get row count

// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

// . . . 

// Batch query to retrieve the COUNT of records and
// all of the records in the Orders table as two result sets.
String sqlText = "SELECT COUNT(*) FROM Orders; " +
 "SELECT * FROM Orders;";

// Create the connection.
SqlConnection conn = new SqlConnection(
 ConfigurationSettings.AppSettings["Sql_ConnectString"]);
SqlCommand cmd = new SqlCommand(sqlText, conn);
conn.Open( );

// Create a DataReader on the first result set.
SqlDataReader dr = cmd.ExecuteReader( );
// Get the count of records from the select count(*) statement.
dr.Read( );
resultTextBox.Text = "Orders table record count, using COUNT(*)= " +
 dr.GetInt32(0) + Environment.NewLine;

// Move to the data result set.
dr.NextResult( );
int count = 0;
// Iterate over the records in the DataReader.
while(dr.Read( ))
{
 count++;

 // . . . Do something interesting with the data here.
}

// Close the DataReader and the connection.
dr.Close( );

resultTextBox.Text += "Orders table record count, " +
 "iterating over result set = " + count +
 Environment.NewLine;

// Create the stored procedure to use in the DataReader.
cmd = new SqlCommand("SP0207_GetOrders", conn);
cmd.CommandType = CommandType.StoredProcedure;
// Create the output paramter to return @@ROWCOUNT.
cmd.Parameters.Add("@RowCount", SqlDbType.Int).Direction =
 ParameterDirection.Output;

// Create a DataReader for the result set returned by
// the stored procedure.
dr = cmd.ExecuteReader( );

// . . . Process the data in the DataReader.

// Close the DataReader.
dr.Close( );
// The output parameter containing the row count is now available.

resultTextBox.Text += "Orders table record count, " +
 "returning @@ROWCOUNT from stored procedure = " + cmd.Parameters["@RowCount"].Value;

conn.Close( );
Comment

PREVIOUS NEXT
Code Example
Csharp :: Razor do while loop 
Csharp :: c# get count from unknown list 
Csharp :: ? in c# 
Csharp :: unity raycast hit child object 
Csharp :: long to binary c# 
Csharp :: check the request comes from which operating system used by user in asp net core 
Csharp :: multiple lines in string c# parameterized 
Csharp :: Permutation and Combination in C# 
Csharp :: attributes C# reflection variable update site:stackoverflow.com 
Csharp :: belgium 251 PRG Prac 
Csharp :: Acrylic UWP Title bar C# 
Csharp :: new bitmap pixel format c# 
Csharp :: find first occurrence of character in string 
Csharp :: listview android studio java 
Csharp :: bitwise and c# 
Csharp :: c# loop array back 
Csharp :: how to check if time is between two timespans in c# 
Csharp :: what is c# used for 
Csharp :: unity public script 
Csharp :: inverse kinematics not working unity 
Csharp :: C# webclient immitate browser 
Csharp :: git set origin 
Html :: blank space html 
Html :: only accept image file input 
Html :: print page button html 
Html :: html character tab 
Html :: center p html 
Html :: add href to div 
Html :: html file only image 
Html :: bootstrap side by side columns 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =