ITKeyword,专注技术干货聚合推荐

注册 | 登录

sql - SqlBulkCopy fails trying to copy a row with large content in an XML column

itPublisher 分享于

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

I am trying to copy a record from one SQL Server table to another. Both tables have the same structure, one of the columns is of type xml. Source table has a large xml content in of the rows, about 2.5Mb.

I saved the content of the xml column into a file, see map.zip attached or download it from https://docs.google.com/leaf?id=0Bz4PXXEQL5TpM2U5MWJhM2MtMTI0Yi00ODg0LTk4OWItMzJiNjVjMDIzNjc2&hl=en&authkey=CLT5i8oP

Simplified version of my code:

string query = "select * from MyTableSource where id = 1";

using (SqlConnection targetConnection = new SqlConnection(connectionStringTarget))
{
targetConnection.Open();

using (SqlConnection sourceConnection = new SqlConnection(connectionStringSource))
{
    sourceConnection.Open();

    using (SqlCommand command = new SqlCommand(query, sourceConnection))
    {
    using (IDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult))
    {
        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(targetConnection))
        {
        bulkCopy.DestinationTableName = "MyTableTarget";
        bulkCopy.WriteToServer(reader);
        }
    }
    }
} 
}

Exception that occurs on bulkCopy.WriteToServer:

System.Data.SqlClient.SqlException was unhandled
Message=XML parsing: Document parsing required too much memory
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=6303
Procedure=""
Server=myserver
State=1
StackTrace:
     at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
     at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
     at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
     at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternal()
     at System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServer(Int32 columnCount)
     at System.Data.SqlClient.SqlBulkCopy.WriteToServer(IDataReader reader)
     at SyncTest.Form1.buttonCopyXml_Click(Object sender, EventArgs e) in C:\..\Form1.cs:line 2251
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
     at System.Windows.Forms.Control.WndProc(Message& m)
     at System.Windows.Forms.ButtonBase.WndProc(Message& m)
     at System.Windows.Forms.Button.WndProc(Message& m)
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
     at SyncTest.Program.Main() in C:\..\Program.cs:line 18
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.ThreadHelper.ThreadStart()
InnerException: 

It looks like a SqlBulkCopy bug. I wonder if somebody else can reproduce and confirm that.

Update: Filed it to Microsoft,

_https://connect.microsoft.com/VisualStudio/feedback/details/614046/sqlbulkcopy-fails-trying-to-copy-a-row-with-large-content-in-an-xml-column

they confirmed this is their bug:

From debugging thus far, looks like an issue with server side processing of XML in the bulk copy path. One of the attributes in the XML file is really large and this is causing SQL Server to fail when processing the XML because of an allocation size limit.

sql sql-server xml sqlbulkcopy
|
  this question
edited Nov 3 '10 at 19:30 asked Oct 15 '10 at 21:52 stask 38 8      Very helpful. This same bug affects bulk imports of large text to XML columns using SSIS. Discovered this on SQL Server 2008 R2. –  epic_fil Jul 11 '12 at 15:03

 | 

1 Answers
1

Change your select query to retrieve the xml column as a [n]varchar(max) column type, to avoid the xml processing overhead, and probably the bug as well.


|
  this answer
answered Jan 28 '11 at 11:34 RobinG 198 6

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。