volvereabhi
volvereabhi

Reputation: 60

Convert SqlDataReader into array

C# code :

string conn = @"Data Source=MRT\SQLSERVER;Initial Catalog=hrm;Persist Security Info=True;User ID=sa;Password=*******";

SqlConnection objsqlconn = new SqlConnection(conn);
objsqlconn.Open();

SqlCommand cmd = new SqlCommand("usp_fetchtest", objsqlconn);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter lastname = cmd.Parameters.Add("@userid", SqlDbType.Int);
lastname.Value = 6;

SqlDataReader sdr;
sdr = cmd.ExecuteReader();

Stored procedure :

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[usp_fetchtest] 
    @userid int
AS
BEGIN
    SELECT firstName, lastName 
    FROM personal  
    WHERE personal.userid = @userid
END

How can I store this value in two array for firstname and lastname? So I can operate on that info in further project steps?

Upvotes: 0

Views: 210

Answers (1)

W. Winnie
W. Winnie

Reputation: 106

You can iterate over the Read method:

while(sdr.Read())
{
    //Use get methods ex. sdr.GetString(columnIndex) or indexer sdr["firstName"] to 
    //extract data and place them in to other objects for further processing.
}

Upvotes: 1

Related Questions