Wrox Home  
Search P2P Archive for: Go

  Return to Index  

ado_dotnet thread: Mapping two tables DataSet to a single database table


Message #1 by "Zort Sid" <zortsid@h...> on Fri, 29 Mar 2002 08:22:04
Hi,
I always used Mapping classes to map columns from a table 
to a DataSet. This time I need to map columns from a 
DataSet having two tables into a single database's table 
but I'm not able to do that :(
Here's the scenario:
 
1) I receive an XML document like this:
<?xml version="1.0" encoding="utf-8"?>
<BOOKS>
  <AUTHOR>
    <FName>John</FName>
    <LName>Smith</LName>
  </AUTHOR>
  <BOOK>
    <Title>My Book</Title>
    <ISBN>115577447X</ISBN>
  </BOOK>
</BOOKS>
 
2) Using the ReadXml() method I fill the DataSet object 
with that document. The operation creates two tables: 
AUTHOR and BOOK
 
3) I have a single table in the database similar to this:
 
Table Books
===========
ID_BOOK      varchar(15) key,
Title     varchar(100),
FirstName varchar(30),
LastName  varchar(30)
 
4) I want to use Mapping classes in order to map AUTHOR's 
columns and BOOK's column whithin the DataSet to the Books 
table's columns.
 
Here follows what I tried to do:
 
try
{
SqlConnection dbConn = new SqlConnection
("server=.;database=MyDB;uid=sa;pwd=");
 
SqlDataAdapter da = new SqlDataAdapter("SELECT ISBN, 
Title, FirstName, LastName FROM Books", dbConn);
 
DataTableMapping dtm = new DataTableMapping
("Table","AUTHOR");
 
dtm.ColumnMappings.Add("FirstName","FName");
dtm.ColumnMappings.Add("LastName","LName");
 
DataTableMapping dtm2 = new DataTableMapping
("Table1","BOOK");
 
dtm2.ColumnMappings.Add("ID_BOOK","ISBN")
dtm2.ColumnMappings.Add("Title","Title");
 
da.MissingMappingAction = MissingMappingAction.Ignore;
da.MissingSchemaAction = MissingSchemaAction.Ignore;
 
da.TableMappings.Add(dtm);
da.TableMappings.Add(dtm2);
 
SqlCommandBuilder sa = new SqlCommandBuilder(da);
 
da.Update(dsSourcePoll);
 
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
 
Executing the code seems that it looks for ISBN and Title 
in the first mapped table without searching in the second 
table where mapping is declared.

Thank you in advance.

  Return to Index