PC Review


Reply
Thread Tools Rate Thread

DataSet - Table names

 
 
Emmanuel Gravino
Guest
Posts: n/a
 
      30th Jul 2003
when using a dataadapter to fill a dataset from a query that returns
multiple tables such as:

Select col1, col2, col3 from tableA
Select col1, col2, col3 from tableB

the DataTable names inside the dataset will not be named with the actual
name.

Is there a way i can go around this?


 
Reply With Quote
 
 
 
 
Emmanuel Gravino
Guest
Posts: n/a
 
      30th Jul 2003
thanks.... but what if i wanted to have it automatically done? i.e. can't
the dataadapter or something else do it? is there any schema information
along with the datatables/dataset or any other property which i could used
AFTER the query is executed?




"VineetBatta" <(E-Mail Removed)> wrote in message
news:092601c3567f$7f090a00$(E-Mail Removed)...
> it defaults to the name specified
>
> If the Query
>
> SELECT * FROM PRODUCTS
>
> //Fill the dataset object.
> sqlData.Fill(inboundTables,"ASD");
>
> So we can use
> DataView dv = ds.Tables["ASD"].DefaultView ;
>
> But if there are multiple Queries then next table name
> will default to ASD1 and so on.....
>
> But what u can do is that call all the subsequent using
> Command Object.
>
> The following code was tested.
>
> try
> {
> SqlConnection sqlConn =
> new SqlConnection("Initial Catalog=NorthWind;Data Source=
> [];User ID=[];Password=[];");
>
> //Open the Connection.
> sqlConn.Open();
>
> //TODO: DataReader can be
> used for faster access.
> DataSet inboundTables =
> new DataSet();
>
>
> string query = "SELECT *
> FROM PRODUCTS";
>
> SqlCommand sqlCmd = new
> SqlCommand();
> sqlCmd.Connection =
> sqlConn;
> sqlCmd.CommandText =
> query;
>
>
> //Create DataAdapter
> object and Attach the SqlCommand Object.
> SqlDataAdapter sqlData =
> new SqlDataAdapter(sqlCmd);
>
>
> //Fill the dataset object.
> sqlData.Fill
> (inboundTables,"PRODUCTS");
>
> query = "SELECT * FROM
> ORDERS";
> sqlCmd.CommandText =
> query;
>
> //Fill the dataset object.
> sqlData.Fill
> (inboundTables,"ORDERS");
>
> sqlConn.Close();
>
> return inboundTables;
> }
> catch(Exception ex)
> {
> throw ex;
> }
>
> -Vineet Batta
> MCAD
>
> VineetBatta
> >-----Original Message-----
> >when using a dataadapter to fill a dataset from a query

> that returns
> >multiple tables such as:
> >
> >Select col1, col2, col3 from tableA
> >Select col1, col2, col3 from tableB
> >
> >the DataTable names inside the dataset will not be named

> with the actual
> >name.
> >
> >Is there a way i can go around this?
> >
> >
> >.
> >



 
Reply With Quote
 
Kathleen Dollard
Guest
Posts: n/a
 
      30th Jul 2003
Emmanuel,

In addition to the mapping Michael's suggestion, you can simply rename the
table.

ds("Table").TableName = "RealTableName"

Unfortunately you can not do this automatically, as Michael explained.

--
Kathleen (MVP-VB)



"Emmanuel Gravino" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> thanks.... but what if i wanted to have it automatically done? i.e. can't
> the dataadapter or something else do it? is there any schema information
> along with the datatables/dataset or any other property which i could used
> AFTER the query is executed?
>
>
>
>
> "VineetBatta" <(E-Mail Removed)> wrote in message
> news:092601c3567f$7f090a00$(E-Mail Removed)...
> > it defaults to the name specified
> >
> > If the Query
> >
> > SELECT * FROM PRODUCTS
> >
> > //Fill the dataset object.
> > sqlData.Fill(inboundTables,"ASD");
> >
> > So we can use
> > DataView dv = ds.Tables["ASD"].DefaultView ;
> >
> > But if there are multiple Queries then next table name
> > will default to ASD1 and so on.....
> >
> > But what u can do is that call all the subsequent using
> > Command Object.
> >
> > The following code was tested.
> >
> > try
> > {
> > SqlConnection sqlConn =
> > new SqlConnection("Initial Catalog=NorthWind;Data Source=
> > [];User ID=[];Password=[];");
> >
> > //Open the Connection.
> > sqlConn.Open();
> >
> > //TODO: DataReader can be
> > used for faster access.
> > DataSet inboundTables =
> > new DataSet();
> >
> >
> > string query = "SELECT *
> > FROM PRODUCTS";
> >
> > SqlCommand sqlCmd = new
> > SqlCommand();
> > sqlCmd.Connection =
> > sqlConn;
> > sqlCmd.CommandText =
> > query;
> >
> >
> > //Create DataAdapter
> > object and Attach the SqlCommand Object.
> > SqlDataAdapter sqlData =
> > new SqlDataAdapter(sqlCmd);
> >
> >
> > //Fill the dataset object.
> > sqlData.Fill
> > (inboundTables,"PRODUCTS");
> >
> > query = "SELECT * FROM
> > ORDERS";
> > sqlCmd.CommandText =
> > query;
> >
> > //Fill the dataset object.
> > sqlData.Fill
> > (inboundTables,"ORDERS");
> >
> > sqlConn.Close();
> >
> > return inboundTables;
> > }
> > catch(Exception ex)
> > {
> > throw ex;
> > }
> >
> > -Vineet Batta
> > MCAD
> >
> > VineetBatta
> > >-----Original Message-----
> > >when using a dataadapter to fill a dataset from a query

> > that returns
> > >multiple tables such as:
> > >
> > >Select col1, col2, col3 from tableA
> > >Select col1, col2, col3 from tableB
> > >
> > >the DataTable names inside the dataset will not be named

> > with the actual
> > >name.
> > >
> > >Is there a way i can go around this?
> > >
> > >
> > >.
> > >

>
>



 
Reply With Quote
 
Michael Lang
Guest
Posts: n/a
 
      30th Jul 2003
It cannot be done automatically from the SQL string. SQL strings can
contain more than one table name in the FROM clause.

The framework does supply a way to "rename" each table in your dataset so
you can access them by name. Actually it is a mapping not a renaming. To
do this use the TableMappings property of the DataAdapter before filling the
DataSet. Add a TableMapping for each table...

//Assuming...
// Select ... from Orders
// Select ... from OrderDetails
myDataAdapter.TableMappings.Add("Table1", "Orders");
myDataAdapter.TableMappings.Add("Table2", "OrderDetails");
myDataAdapter.Fill(myDataSet);
myDataTableOrders = myDataSet["Orders"];
myDataTableOrderDetails = myDataSet["OrderDetails"];

Michael Lang

"Emmanuel Gravino" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> thanks.... but what if i wanted to have it automatically done? i.e. can't
> the dataadapter or something else do it? is there any schema information
> along with the datatables/dataset or any other property which i could used
> AFTER the query is executed?
>
>
>
>
> "VineetBatta" <(E-Mail Removed)> wrote in message
> news:092601c3567f$7f090a00$(E-Mail Removed)...
> > it defaults to the name specified
> >
> > If the Query
> >
> > SELECT * FROM PRODUCTS
> >
> > //Fill the dataset object.
> > sqlData.Fill(inboundTables,"ASD");
> >
> > So we can use
> > DataView dv = ds.Tables["ASD"].DefaultView ;
> >
> > But if there are multiple Queries then next table name
> > will default to ASD1 and so on.....
> >
> > But what u can do is that call all the subsequent using
> > Command Object.
> >
> > The following code was tested.
> >
> > try
> > {
> > SqlConnection sqlConn =
> > new SqlConnection("Initial Catalog=NorthWind;Data Source=
> > [];User ID=[];Password=[];");
> >
> > //Open the Connection.
> > sqlConn.Open();
> >
> > //TODO: DataReader can be
> > used for faster access.
> > DataSet inboundTables =
> > new DataSet();
> >
> >
> > string query = "SELECT *
> > FROM PRODUCTS";
> >
> > SqlCommand sqlCmd = new
> > SqlCommand();
> > sqlCmd.Connection =
> > sqlConn;
> > sqlCmd.CommandText =
> > query;
> >
> >
> > //Create DataAdapter
> > object and Attach the SqlCommand Object.
> > SqlDataAdapter sqlData =
> > new SqlDataAdapter(sqlCmd);
> >
> >
> > //Fill the dataset object.
> > sqlData.Fill
> > (inboundTables,"PRODUCTS");
> >
> > query = "SELECT * FROM
> > ORDERS";
> > sqlCmd.CommandText =
> > query;
> >
> > //Fill the dataset object.
> > sqlData.Fill
> > (inboundTables,"ORDERS");
> >
> > sqlConn.Close();
> >
> > return inboundTables;
> > }
> > catch(Exception ex)
> > {
> > throw ex;
> > }
> >
> > -Vineet Batta
> > MCAD
> >
> > VineetBatta
> > >-----Original Message-----
> > >when using a dataadapter to fill a dataset from a query

> > that returns
> > >multiple tables such as:
> > >
> > >Select col1, col2, col3 from tableA
> > >Select col1, col2, col3 from tableB
> > >
> > >the DataTable names inside the dataset will not be named

> > with the actual
> > >name.
> > >
> > >Is there a way i can go around this?
> > >
> > >
> > >.
> > >

>
>



 
Reply With Quote
 
Michael Lang
Guest
Posts: n/a
 
      30th Jul 2003
That is a problem. I tried that before I went to table mappings. I was
confused when the following caused an error.

ds("Table").TableName = "RealTableName"; //< -- no problem, it is writeable.
DataTable dt = ds("RealTableName"); //< -- fails

I was told that only a TableMapping will allow the line above to work.
Updating the TableName property of a DataTable does not update the key in
the container DataSet. Which makes sense. What collection lets you change
a key of an item after it is added?

Michael Lang, MCSD

"Kathleen Dollard" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Emmanuel,
>
> In addition to the mapping Michael's suggestion, you can simply rename the
> table.
>
> ds("Table").TableName = "RealTableName"
>
> Unfortunately you can not do this automatically, as Michael explained.
>
> --
> Kathleen (MVP-VB)
>
>
>
> "Emmanuel Gravino" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > thanks.... but what if i wanted to have it automatically done? i.e.

can't
> > the dataadapter or something else do it? is there any schema information
> > along with the datatables/dataset or any other property which i could

used
> > AFTER the query is executed?
> >
> >
> >
> >
> > "VineetBatta" <(E-Mail Removed)> wrote in message
> > news:092601c3567f$7f090a00$(E-Mail Removed)...
> > > it defaults to the name specified
> > >
> > > If the Query
> > >
> > > SELECT * FROM PRODUCTS
> > >
> > > //Fill the dataset object.
> > > sqlData.Fill(inboundTables,"ASD");
> > >
> > > So we can use
> > > DataView dv = ds.Tables["ASD"].DefaultView ;
> > >
> > > But if there are multiple Queries then next table name
> > > will default to ASD1 and so on.....
> > >
> > > But what u can do is that call all the subsequent using
> > > Command Object.
> > >
> > > The following code was tested.
> > >
> > > try
> > > {
> > > SqlConnection sqlConn =
> > > new SqlConnection("Initial Catalog=NorthWind;Data Source=
> > > [];User ID=[];Password=[];");
> > >
> > > //Open the Connection.
> > > sqlConn.Open();
> > >
> > > //TODO: DataReader can be
> > > used for faster access.
> > > DataSet inboundTables =
> > > new DataSet();
> > >
> > >
> > > string query = "SELECT *
> > > FROM PRODUCTS";
> > >
> > > SqlCommand sqlCmd = new
> > > SqlCommand();
> > > sqlCmd.Connection =
> > > sqlConn;
> > > sqlCmd.CommandText =
> > > query;
> > >
> > >
> > > //Create DataAdapter
> > > object and Attach the SqlCommand Object.
> > > SqlDataAdapter sqlData =
> > > new SqlDataAdapter(sqlCmd);
> > >
> > >
> > > //Fill the dataset object.
> > > sqlData.Fill
> > > (inboundTables,"PRODUCTS");
> > >
> > > query = "SELECT * FROM
> > > ORDERS";
> > > sqlCmd.CommandText =
> > > query;
> > >
> > > //Fill the dataset object.
> > > sqlData.Fill
> > > (inboundTables,"ORDERS");
> > >
> > > sqlConn.Close();
> > >
> > > return inboundTables;
> > > }
> > > catch(Exception ex)
> > > {
> > > throw ex;
> > > }
> > >
> > > -Vineet Batta
> > > MCAD
> > >
> > > VineetBatta
> > > >-----Original Message-----
> > > >when using a dataadapter to fill a dataset from a query
> > > that returns
> > > >multiple tables such as:
> > > >
> > > >Select col1, col2, col3 from tableA
> > > >Select col1, col2, col3 from tableB
> > > >
> > > >the DataTable names inside the dataset will not be named
> > > with the actual
> > > >name.
> > > >
> > > >Is there a way i can go around this?
> > > >
> > > >
> > > >.
> > > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add Database Names, table names & related fields from table in combo box Snowy Microsoft Excel Programming 4 29th Apr 2006 01:08 PM
copying a datatable content from an untyped dataset into a table which is inside a typed dataset Nedu N Microsoft ADO .NET 2 31st Oct 2003 01:05 PM
Parse table from one dataset and create new dataset using this table moondaddy Microsoft ADO .NET 2 22nd Oct 2003 11:38 PM
Dataset and table names..... Jim Microsoft ADO .NET 3 30th Jun 2003 11:34 AM
Dataset and table names..... Jim Microsoft C# .NET 3 30th Jun 2003 11:34 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:03 AM.