1 connection- 2 result tables

A

Avon

Hi there,

I am very sorry for my not perfect English.

I've got very simple question. Is it possible to have stored procedure with
2 result tables and then from my application to refer to this tables by ado.

Something likes that:

Select * from aaa

Select * from bbb



And then in my application

SqlDataAdapter adapter=new SqlDataAdapter();

DataSet MyDataSet=new DataSet();

adapter.Fill(MyDataSet, "aaa");

adapter.Fill(MyDataSet, "bbb");



Basically I want to receive with 1 connection to the database 2 tables.



If is possible haw I can do it,

And if is not possible is there any other way I can do it?

Thanks in advance
 
P

Peter Rilling

Yes.

You would then refer to it using syntax such as myDataset.Tables[0] or
myDataset.Tables[1].

The tables are just indexed in the table collection and can be accessed.
 
G

Guest

Hello Dear Avon,

Yes You can have a stored procedure with 2 SQL Select Statement.
I will show you how you can also retreive 2 SQL Select Statements in one
single Query.

It goes like this.

Create a connection string

const string dsn="server=(local);trusted_connection=yes;database=pubs";

Using(SqlConnection conn = new SqlConnection(dsn);

Using(SqlCommand cmd = new SqlCommand(SELECT * FROM Authors;SELECT * FROM
Publishers", conn));

//here you can give two (2) SQL Statements with a semicolon (;) in-between
like above.

SqlDataReader dr = cmd.ExecuteReader();


do
{
while (dr.read())
{
Console.Writeline(" {0} {1}", dr[1], dr[2]);
}

}while(reader.NextResult[1])
}

************************************************************************************************************************

You can also fill the DataSet with 2 Tables like below

Create a connection string

const string dsn="server=(local);trusted_connection=yes;database=pubs";

Using(SqlConnection conn = new SqlConnection(dsn);

SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Authors;SELECT * FROM
Publishers", conn);


then you can fill the DataSet like this
=======================
DataSet ds = new DataSet();

da.Fill(ds,"Authors");

//The Table Name "Authors" is assigned to the First (1st) DataTable. You
have to Specifically Assign the Table Name for the Second (2nd) DataTable
like below

ds.Tables[1].TableName="Publishers"

you can user the DataTable by refereing either their Ordinal Number or Name
of the Table like below

ds.Tables["Authors"]
or
ds.Tables[0]

AND / OR

ds.Tables["Publishers"]
or
ds.Tables[1]

For anything and everything please let me know

bye
Venkat_KL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top