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
C#
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;
namespaceSqlBulkCopy1
{
publicpartialclassForm1 : Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_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);
}
}
}
5 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Hi
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?
no, im a newbie in everything, dont know much about sqlserver
about the xmlfile, that is how it is from the source i dl from, its about 2000 of that structure
Thank you Bechir!
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. ;-)
Bechir BejaouiRank #144 · 15.9K reputationPosted Sep 17, 2010, 9:02 AM EDT
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 BejaouiRank #144 · 15.9K reputationPosted Sep 16, 2010, 4:15 PM EDT
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?
Kenneth Johansson9 reputationPosted Sep 16, 2010, 11:42 PM EDT
about the xmlfile, that is how it is from the source i dl from, its about 2000 of that structure
Kenneth Johansson9 reputationPosted Sep 17, 2010, 11:12 AM EDT
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. ;-)
Bechir BejaouiRank #144 · 15.9K reputationPosted Sep 18, 2010, 2:51 AM EDT
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