Please try if the following code can work for you.
Because the first four properties are fixed, we only need to process the following data.
static void Main(string[] args)
{
string connString = @"";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand command = new SqlCommand("select * from Test1", conn))
{
SqlDataReader dataReader = command.ExecuteReader();
List<Data> datas = new List<Data>();
while (dataReader.Read())
{
string section = dataReader.GetString(1);
string lineitem = dataReader.GetString(1);
string brokerCode = dataReader.GetString(2);
string brokerName = dataReader.GetString(3);
int count = dataReader.FieldCount;
for (int i = 4; i < count; i++)
{
Data data = new Data()
{
Section = section,
Lineitem = lineitem,
BrokerCode = brokerCode,
BrokerName = brokerName,
Period = dataReader.GetName(i),
PeriodValue = dataReader.GetString(i)
};
datas.Add(data);
}
}
Console.WriteLine();
}
}
I didn't use a stored procedure, but this should be easy to modify.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.