简体   繁体   中英

how to get data from two table in the grid by using stored procedure in c#

I have create a stored procedure with name getCusnmae1

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[getCusnmae1]
AS
BEGIN
    SELECT ALL * FROM dbo.Customers;
    SELECT ALL * FROM dbo.Order1;
END

now what i have done so far that create a property class Customer and want to display data on the grid

I wrote the code for this is:

conn1.ConnectionString = " server=.\\ms2k5;database=Inventory;Trusted_Connection=true";
            conn1.Open();
            SqlCommand testCMD = new SqlCommand ("getCusnmae1", conn1);
            testCMD.CommandType = CommandType.StoredProcedure;
            SqlDataReader myReader = testCMD.ExecuteReader();
            List<Customer> list = new List<Customer>();
            while (myReader.Read())
            { 
                Customer md = new Customer();
                md.CustName = myReader.;
                md.custname = myReader.GetValue(1).ToString();
                md.contact = myReader.GetValue(2).ToString();
                list.Add(md);
            } 

by which I get column value only from the Customers Table what i Have to do to get data from child table Order1

When I write

 md.contact = myReader.GetValue(3).ToString();

to acess order table it does not work..

please help thanks

Use SqlDataReader.NextResult() method to handle multiple Results.

SqlDataReader myReader = testCMD.ExecuteReader();
while (myReader.Read())
 { 
  //
 } 
myReader.NextResult();
while (myReader.Read())
 { 
  //
 } 

By default, the data reader is positioned on the first result. For fetch data from others result use

while (myReader .NextResult())
{}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM