将 SqlDataReader 转换为数组

Convert SqlDataReader into array

C# 代码:

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();

存储过程:

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

如何将此值存储在名字和姓氏的两个数组中?所以我可以在进一步的项目步骤中对该信息进行操作?

您可以迭代 Read 方法:

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.
}