My SQL stored procedure is as follows:
CREATE PROCEDURE [dbo].[GetTradeByLevel](
@LevelId INT
)
AS
BEGIN
if(@LevelId=1)
BEGIN
select * from CHANNELCLASS
select a.* from accountmaster a join CHANNELCLASS b on a.ChannelClassificationId=b.ChannelClassificationId
select r.* from REGION r join accountmaster a on r.Accountid=a.AccountID
select s.* from STORE s join REGION r on s.RegionID=r.RegionId
END
else if(@LevelId=2)
BEGIN
select a.* from accountmaster a join CHANNELCLASS b on a.ChannelClassificationId=b.ChannelClassificationId
select r.* from REGION r join accountmaster a on r.Accountid=a.AccountID
select s.* from STORE s join REGION r on s.RegionID=r.RegionId
END
else if(@LevelId=3)
BEGIN
select r.* from REGION r join accountmaster a on r.Accountid=a.AccountID
select s.* from STORE s join REGION r on s.RegionID=r.RegionId
END
else if(@LevelId=4)
BEGIN
select s.* from STORE s join REGION r on s.RegionID=r.RegionId
END
END
GO
I have to call this stored procedure in C# console application. If my parameter value is 1 it means I have to store 4 table values in a C# List<>. I have tried, but it stores only the first table’s data.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int v = 1;
int cl;
SqlConnection con = new SqlConnection("Data Source=CTSINDLFVMOSS;Initial Catalog=DB_CGTPO_DEVE;User ID=DB_CGTPO_DEVE;Password=cgtpo");
con.Open();
SqlCommand cmd = new SqlCommand("GetTradeByLevel", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@LevelId", v);
List<string> customers = new List<string>();
using (SqlDataReader dr = cmd.ExecuteReader())
{
cl = dr.FieldCount;
while (dr.Read())
{
for (int i = 0; i < cl; i++)
{
customers.Add(dr[i].ToString());
//customers.Add(dr[1].ToString());
//customers.Add(dr[2].ToString());
}
}
}
for (int i = 0; i < customers.Count; i++) // Loop through List with for
{
Console.WriteLine(customers[i]);
}
con.Close();
}
}
}
This is my console application page… if my parameter value is 1 mean it have to store 4 table values but it will taking first table value only i have to store all 4 table values
any one help me,….
yupes NextResult()
example:
if you perform multiple selects in your stored procedure, all will be contained in the
SqlDataReader, it’s just your method of advancing the reader…http://www.mindfiresolutions.com/NextResult-in-C-or-ADONET-data-reader-705.php