I have the same names as xml in database, have tried with different dbtypes as int, float, nvarchar etc.
sometimes it copy only the first 2 elements in the xmldocument. I got an error that says "The given ColumnName 'omgang' does not match up with any column
in data source". And sometimes the "omgang" column is copied but with wrong value. I also tried with ColumnMappings without success.
Im using VS 2010 Express C# with Framework 2.0, SqlServer 2008 R2, SSMS.
http://netken.se/test/id=1&omgang=3484.xml
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Xml; using System.Data.SqlClient; namespace SqlBulkCopy1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=AK\\SQLEXPRESS;Initial Catalog=Test3;Integrated Security=True"; DataSet ds = new DataSet(); DataTable sourcedata = new DataTable(); ds.ReadXml(@"http://netken.se/test/id=1&omgang=3485.xml"); sourcedata = ds.Tables[0];
using (SqlConnection sqlconn = new SqlConnection(connectionString)) { sqlconn.Open(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlconn)) {
bulkCopy.DestinationTableName = "Tbl_Test3"; bulkCopy.WriteToServer(sourcedata); } } }
|


Bechir BejaouiPosted Sep 17, 2010, 1:02 PM
I strongly advise you to leave everything and start by reading those articles in order
Xml overview
http://www.w3schools.com/xml/xml_whatis.asp
xsd overview
http://www.w3schools.com/schema/default.asp
Bulk insert
http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/
Open row set
http://jasonhaley.com/blog/post/2004/03/06/OPENROWSET-T-SQL-function.aspx
bcp utility
http://msdn.microsoft.com/en-us/library/ms162802(SQL.90).aspx
Additional resources those are importants for your case
http://blog.sqlauthority.com/2010/06/23/sqlauthority-news-guest-post-select-from-xml-jacob-sebastian/
http://blog.sqlauthority.com/2009/12/02/sql-server-validate-an-xml-document-in-tsql-using-xsd-by-jacob-sebastian/
This is very very very imporant to know before starting using the Bulk copy class in your case.
Bechir BejaouiPosted Sep 18, 2010, 6:51 AM
Sure, you'll propose a better solution than you though before as you auquire new consistent tools to do that
Those are my related articles, I excpect your comments
http://www.c-sharpcorner.com/Authors/AuthorDetails.aspx?AuthorID=yougerthen
Kenneth JohanssonPosted Sep 17, 2010, 3:12 PM
I guess its better to start from the beginning, even if it seems boring many times.
So i will take the time and learn from the links first, then i will show you the answer myself maybe. ;-)
Kenneth JohanssonPosted Sep 17, 2010, 3:42 AM
about the xmlfile, that is how it is from the source i dl from, its about 2000 of that structure
Bechir BejaouiPosted Sep 16, 2010, 8:15 PM
I think the problem is the xml data structure, as the omgang is nested witihn ohter element.
I have two questions for you have you advanced knowlege on sql server or not, so that I can follow the best manner to help you, and it is possible to retructure the xml file or not?