Need to load multiple sql server tables from C# app - c#

Hi I have a pretty complex schema (.xsd) and an .xml files. Some nodes have about 70 attributes and some 10-15. I used xsd2code to create c# classes and then deserialized my xml file. I created a database based on my .xsd class using xsd2db application. So I've read my xml file and need to store it into the database. I have 74 classes/tables with primary/foreign key relationships. I looked into SqlBulkCopy - for that it looks like I need to convert everything into datatables/rows/columns. And I will need to take into account the hierarchy for satisfying foreign keys. Is there another way of doing it? What would be the best (fastest) approach?
Thank you
Jenny

There are different ways to import a XML file either through T-SQL, SSIS, VB Script, etc.
Sorry to just google and post links, but I think sql squirrel has found the easiest method for getting an XML file into SQL Server 2005 is to use T-SQL:
http://www.lockergnome.com/sqlsquirrel/2008/05/22/how-to-import-a-xml-file-into-sql-server-2005/
http://www.lockergnome.com/sqlsquirrel/2008/05/23/how-to-turn-imported-xml-into-a-relational-format-in-sql-server-2005/
The SQLTeam seem to think T-SQL is the WTG as well:
http://weblogs.sqlteam.com/mladenp/archive/2007/06/18/60235.aspx

Related

How do I transform a xsd file into a SQL Server table?

I would like to turn a XSD file into a SQL Server table. Is there anyway to do this without using the tool XSD2DB because I already tried it and does not work .
In fact what I would like to do is to persist information of an object in the database. I already have a class that I created from XSD and it has about 400 properties. I don't want to have to manually create 400 columns in a table database ...
I've already tried to create a code that does an INSERT, but I don't think it is the best way to do it; it would be huge ugly and inefficient. I could use Entity Framework however with the tutorials that I've seen on the Internet you have to create a Data Model based on a previously existing table in the database ... Is there any other way to do this ?
Thank you all!

Transform (large) XML-files into relational SQL

I've been tasked with the job of importing a set of XML files, transform them and upload them to an SQL database, and then re-transforming them to a different XML-format.
The XML files are rather large, and some of them a little complex, so I'm unsure of the best way to do this. I'd of course like to automate this process somehow - and was actually hoping there'd be some kind of Entity Framework-esque solution to this.
I'm quite new to handling and dealing with XML in .NET, so I don't really know what my options are. I've read about XSLT, but that seems to me, to be a "language" I need to learn first, making it kind of not a solution for me.
Just to set a bit of context, the final solution actually needs to import new/updated versions of the XML on a weekly basis, uploading the new data to sql, and re-exporting as the other XML-format.
If anyone could give me any ideas as to how to proceed, I'd be much obliged.
My first instict was to use something like XSD2DB or XML SPY to first create the database structure, but I don't really see how I'm then supposed to proceed either.
I'm quite blank in fact :)
XSLT is language used by XML processors to transform XML document in one format to XML document in another format. XSLT would be your choice if you don't need to store data in database as well.
All tools like XSD2DB or XML SPY will create some database schema for you but the quality of the schema will be very dependent on quality of XML document and XSD (do you have XSD or are you going to generate it from sample XML?). The generated database will probably not be to much useful for EF.
If you have XSD you can use xsd.exe tool shipped with Visual studio and generate classes representing data of your XML files in .NET code. You will be able to use XmlSerializer to deserialize the XML document into your generated classes. The problem is that some XSD constructs like choice are modeled in .NET code by very ugly way. Another problem can be performance if your XML files are really huge because deserialization must read all data at once. The last problem can be again EF - classes generated by XSD will most probably not be usable as entities and you will not be able to map them.
So either use EF and in such case you will have to analyze XSD and create custom entities and mapping to your own designed database and you will fill your classes either from XmlReader (best performance), XmlDocument or XDocument or use some tool helping you creating classes or database from XML and in such case use direct SQL to work with a database.
Reverse operation will again require custom approach. You will have data represented either by your custom EF entities or by some autogenerated classes and you will have to transform them to a new format. You can again use xsd.exe to get classes for a new format and write a custom .NET code filling new classes from old ones (and use XmlSerializer to persist a new structure to XML) or you can use XmlWriter, XDocument or XmlDocument to build target XML document directly.
Data migration in any form is not easy task with ready to use solution. In case of really huge data processing you can use tools like SQL Server Integration Services where you will interact with XML and SQL directly and process data in batches.
Have a look at SQLXML 4.0. It does exactly what you want (in upload part).

How to effectively save database table names in XML and parse them in C#

I have a requirement to copy the tables from one DB to another . The table names (to be used in the application) are configured in the XML file.
This works out if the tables doesn't have any relations. In case any of the tables have relations. What is the best way to proceed?
How to decide the XML schema? (how to specify master tables, if they are in turn dependent on other tables)
How to effectively parse the XML schema in C#, so that the master tables are loaded first.
Thanks,
Vijay
2.How to effectively parse the XML schema in C#, so that the master tables are loaded first.
NOT AT ALL. This is a toy data set (i.e. small), just use the Xml Serializer. No need to write parser.
1.How to decide the XML schema? (how to specify master tables, if they are in turn dependent on
other tables)
Experience, requrements and KISS principle. Sorry, this is so basic I dont know what else to write.
Using linq-to-sql, it is possible to have an external mapping in xml format between the classes and their properties and the table and column names they map to. Linq-to-sql have features for foreign key references etc. built in.
Using an external mapping source, you will have to do some work by hand. The basic steps are:
Create the database if not already created.
Write classes with the names that you want to use in the code. You could possibly use the SqlMetal tool to generate raw files that you can strip from mapping attributes.
Write an xml mapping file.
If you are not familiar with linq-to-sql I would recommend you to start with a toy project using the Visual Studio Designer, to quickly get started to get familiar with the technology.

Store Relational database as XML

I have to store relational data (text, numbers and pictures) into an XML file. I want to maintain the relationship between data and the tables structure.
How do I go about doing that?
My Windows Mobile application use Windows Communication Foundation (WCF) to get data. Once it gets the data it stores them into an XML file.
The WCF use ADO.NET Entity Framework to data retrieve.
I'm going to use SQL Server 2008.
The data will be use to show it on application. I don't want to use SQL Server CE to store data because it's slower. Maybe, instead of using XML file I can use text plain files to store texts and numbers, and image files to store images.
In short, XML will be a way to store the data instead using SQL Server CE or WCF.
Thank you!
Thanks for the additional detail.
My suggestion is first, try SQL Server CE. It might not actually be slower for what you're doing. The time you save in not reinventing the wheel might be better spent in speeding up other parts of your application.
For the rest, assuming you don't need to store the data for long, nor do much with it, I'd suggest copying the data from the Web Service return type into a DataSet. The DataSet type has a WriteXml method that can save the data, and a ReadXml method that can read it back in.
While it's a little ambiguous what is needed, you can try looking into 'FOR XML EXPLICIT' under SQL Server. This will include relationships. Link: http://www.eggheadcafe.com/articles/20030804.asp
what about a dataset ? define the tables and relations, load the data into the dataset, and save it as XML (WriteXml method)
Define your XML format as an XSD. If you use Visual Studio use add new DataSet and the "custom tool" will automagically create a strong-typed set of classes based on DataSet. You can define tables and relations with constraints. It is easy to do in Visual Studio. If you are Visual Studio averse, you can create the XSD by hand and use xsd.exe to generate your code.
Once you have the strong-typed DataSet filled with data you can serialize it to an XML stream by calling WriteXml.
What do you mean by, "maintain the relationship between data and the tables structure"? Are you talking about more than maintaining the ID of a source database record in an attribute of an XML node? What do you need to do with the XML data once you have it? Will it be edited and have to be re-applied to the database?
More questions than answers from me....sorry.
What SQL Server are you using? Some servers allow you to request an XML response for a query. That could save you a lot of work.
EDIT: Since you have specified that you will use MSSQL 2008, you may be in luck. In MSSQL 2005 and later there is a "FOR XML" keyword which allows you to get results as XML. Give it a try.

Map relational database to XML in C#

I have an application that needs to map relational database to XML.
The issue is that the database structure is given (I cannot change it) and the XML Schema is given (comes from WSDL).
I need to let the user of my application to tell the system that a given column in a table is mapped to a given element in the schema. Then my application needs to crate XMLs based on the database data.
Is there a technology that can help me? is there a C# control for that.
Thanks
XSLT.
I think you should dynamically (based on user input) create xslt and transform data from database (dataset?) to xml.
What's your database - SQL Server? You can achieve a lot of XML flexibility with the SELECT .... FOR XML statement in SQL Server (most definitely in 2005 and up).
And if that's not flexible enough, then I'd second "peeles" recommendation - do a SELECT...FOR XML first and transform the output using XSLT.
Marc
Select...For sounds simpler; but LINQ in your C# is probably another option.

Categories