如何在使用sqlbulkcopy将大型xml(大小为100 mb)存储到数据库时对数据类型进行类型转换 [英] how to typecast datatype when storing large xml(size 100 mb) into database using sqlbulkcopy

查看:97
本文介绍了如何在使用sqlbulkcopy将大型xml(大小为100 mb)存储到数据库时对数据类型进行类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有大的xml文件,大小是100 MB。

我们知道bydefault所有的xml文件值都是nvarchar类型

我的数据库表列有一些int数据类型,date数据类型。

目前我使用sqlbulkcopy将xml文件数据存储到数据库表中。

这里我得到数据类型不匹配错误。所以这里的解决方案是datatye的类型。

如何使用sqlbulkcopy进行类型转换。任何人都可以解释我的通过代码示例。

这里是代码片段,如下所示

Hi,

I have large xml file and its size is 100 mb.
we know that bydefault all xml file value are nvarchar type
My database table column have some int datatype, date datatype.
currently I am storing xml file data into database table using sqlbulkcopy.
here i am getting datatype mismatch error. so here solution is typecast of datatye.
how i do typecast using sqlbulkcopy. can anybody explain my via code sample.
here is code snippet which is given below

DataSet ds = new DataSet();
DataTable sourceData = new DataTable();
ds.ReadXml(@"..\..\XML\edition_report20140822.xml");
sourceData = ds.Tables[0];
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection,sqlBulkCopyOptions.Default, transaction))
{

       // bulkCopy.ColumnMappings.Clear();
       //column mappings
       //bulkCopy.ColumnMappings.Add("edition_eanisbn13", "szISBN");
       //bulkCopy.ColumnMappings.Add("temp_edition_format", "szFormat");                                                                                 //bulkCopy.ColumnMappings.Add("temp_binding_short_name","szFormatAbbreviation");     
       //bulkCopy.ColumnMappings.Add("edition_product_type", "szMedia");  
       bulkCopy.DestinationTableName = "tbltest;;
       bulkCopy.WriteToServer(copyDataTable);
}





提前致谢



Thanks in advance

推荐答案

首先尝试使用较小的块来查看一切正常。然后将XML拆分为较小的块(可以使用二叉搜索树;))来查找哪个部分包含损坏的数据。



我将它分成两半,然后尝试导入四个中的每一个。然后重复一遍,直到找到错误的确切位置。



无论如何,如果没有其他原因,最好在尝试进行此类导入之前拆分文件,但要缩短获取错误所需的时间。:)



很可能你有数字列中的N / A或者你期望为整数(0/1)的'Y'/'N'。
First try with smaller chunks just to see that everything works. Then split your XML into smaller chunks (you could use binary search tree ;) ) to find which part holds corrupt data.

I'd split it halfway twice and then try to import each of the four. Then repeat that until you find exactly where the error is.

Anyhow, it would be better to split the file before attempting such import if for no other reason but to shorten the time it takes to get to the error. :)

Most probably you have something like N/A in numeric column or 'Y'/'N' in what you expect to be integer (0/1).


这篇关于如何在使用sqlbulkcopy将大型xml(大小为100 mb)存储到数据库时对数据类型进行类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆