Create One DataTable from Many within a DataSet (C# + XML) - c#

I'm attempting to ingest multi-layered XML data from an API in order to insert it into a database.
Firstly, once I've pulled the XML file from the API, I'm using DataSet's ReadXML method to create DataTables. There happens to be 19 in total within the DataSet once ingested.
I understand that I can use DataRelation to link all of the tables, but it looks like the ReadXML method has already inferred some schema info and thus generated what appear to be sensible relationships between each DataTable of the DataSet.
However, I'd like to end up with just one table to port into my database, not 19, and I'd prefer to join/merge/relate the tables in C#, instead of within SQL.
Can you advise on a best practice for creating a single table from all related DataTables within my current DataSet?

Related

Are .NET DataTables useful beyond as a means of interfacing with databases?

I often use the DataTable class in my .NET WCF services, since many of our SPs require TVPs. As far as I know, DataTables are the only way of passing TVPs to SPs.
It just occurred to me that similarly to how tables, in which information is stored according to rows and columns are useful, that the DataTable class may be useful beyond just as a means of interfacing with SQL Server TVPs.
Actually... thinking about this, I have previously written code that iterated over a DataTable's rows, building up an HTML string. However the main reason we used a DataTable as because the same table could be passed to SQL Server as a TVP.
Looking at the docs: https://msdn.microsoft.com/en-us/library/system.data.datatable%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396, it looks like you can effectively create relational object models using DataTables.
Would using DataTables be an effective way of caching data retrieved from a SQL Server in a service?
Another potential use-case that comes to mind... Would there be any benefit of using a DataTable for a collection instead of List<MyType>?
Datatables are slower than Lists/Enumerables, and its better to use dataAdapter while reading data if you really care about performance.
But Datatables can be really useful as a item source for grids, where you want to just publish whole table data on the UI and no need to specify each column individually as in the case of List.

Integrating Database Functionality

I am looking for a few suggestions on how I can integrate database functionality into my WPF application for fast and efficient query processing. Are there libraries I can integrate, or does C# have this built in.
What I intend to have is several tables with less than one thousand entries each. This data is read from an XML file when the application launches and is inserted into tables. All data should reside in memory so no filesystem support is necessary.
Look at ADO.net's DataSet: http://msdn.microsoft.com/en-us/library/ss7fbaez.aspx (emphasis mine):
The ADO.NET DataSet is a memory-resident representation of data that
provides a consistent relational programming model regardless of the
source of the data it contains. A DataSet represents a complete set of
data including the tables that contain, order, and constrain the data,
as well as the relationships between the tables.
There are several ways of working with a DataSet, which can be applied
independently or in combination. You can:
Programmatically create a DataTable, DataRelation, and Constraint within a DataSet and populate the tables with data.
Populate the DataSet with tables of data from an existing relational data source using a DataAdapter.
Load and persist the DataSet contents using XML. For more information, see Using XML in a DataSet (ADO.NET).
See http://msdn.microsoft.com/en-us/library/fx29c3yd.aspx for specifics on "Loading a DataSet from XML".
This is available by default in ADO.Net - one of the base .Net libraries, so it is immediately usable from C# without any additional dependencies. (Technically, C# is just a language - it doesn't provide any libraries.)
You can use ADO.NET Datasets from the xml files. Take a look here for a example
DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable("table1");
dataTable.Columns.Add("col1", typeof(string));
dataSet.Tables.Add(dataTable);
string xmlData = "<XmlDS><table1><col1>Value1</col1></table1><table1> <col1>Value2</col1></table1></XmlDS>";
System.IO.StringReader xmlSR = new System.IO.StringReader(xmlData);
dataSet.ReadXml(xmlSR, XmlReadMode.IgnoreSchema);

Import XML file into SQL server database

How do I import an XML file into a SQL Server Database using C# and .NET in a windows application?
The table name and column names of the SQL Server have to be automatically created (I saw somewhere that they were mapping explicitly every column of the XML table to the already created SQL table's columns).
Appreciate your help.
Sam.
I kind of think this is two questions. So it kind of gets two answers.
1) You would need to parse out the name of the nodes to create column names. Depending on how "deep" your XML is, it may need multiple tables. You'd then connect to your database and run sql statements through code (SqlConnection and SqlCommand objects) to create the tables with the appropriate columns. (CREATE TABLE Blah COLUMNS Bleh, for instance). There is also a SQL Specific API (I think) the name of which I can't recall that will let you do that in a slightly more object-oriented way. The gist is the same: marshal your table and column names, create your table(s) with the appropriate columns.
2) You then have a couple of options- the best would probably be to marshal DataSets from the XML (plenty of examples exist) and then use XML Bulk Insert to upload the whole kit-and-caboodle to SQL all at once.
Let me just say, though, that this is a really ugly solution. SSIS would probably be able to do the same thing for more easily if that's an option.

DataSet and DataTable

What is dataset and datatable?
How both of them are related to each other.
And what are the uses of it.
I also want to know how to create a dataset manually and display the data in the web page using c sharp.
As i am newbie to .net and c sharp
A data set is a collection of data tables and the relationships between them. It normally represents a set of database tables and the relationships (foreign keys) between them. See DataSet on MSDN.
A data table is usually a representation of a database table, in memory. See DataTable on MSDN.
The uses are that you do not need to go to the database every time you want to lookup a value.
The rest of your question is rather broad and not really answerable, as there are many ways to create a dataset in memory. You need to be much more specific regarding what you are trying to do.
A DataSet is an in memory representation of data,It containing one or more DataTables.
A DataTable is an in-memory representation of data, typically retrieved from a database or XML source.
As far as I remember, a DataSet is a collection of DataTables and the Relations between tables.
A DataTable is an object that represents a table of data similar to a database table.
If I were you I'd avoid the use of DataSets and DataTables as they are fairly heavyweight objects and for simple display you don't need any of the extra features that they provide.
Google for information about DataReaders and maybe DataReaders vs DataTables

How to Convert complex XML structures to DataSet with multiple tables

What is the best way to convert an XML document to a .NET 2.0 DataSet. The XML document contains complex structures with parent-child relationships and should be transformed into multiple tables in the DataSet. The DataSet tables should also maintain the relationship among tables.
right now, I've to write custom XSLT to do this transformation
The best (meaning easiest) way, that also includes all the parent/child relationships is...
DataSet myDataSet = new DataSet();
myDataSet.ReadXml("myXmlFile.xml");
Tada!
BTW, if you have the XML in memory (and not in a file), then you can use one of the "ReadXml" overloads to read it from a stream.

Categories