我遇到了SqlBulkCopy(在c#,框架4.5.1中)的问题,其中有效的XML字符串导致错误System.Data.SqlClient.SqlException:{“XML解析:第1行,字符29,字符串文字期待“}”
表格是......
CREATE TABLE [dbo].[JobManager_BigTextTest](
[PKID] [int] IDENTITY(1,1) NOT NULL,
[BigXML] [xml] NOT NULL, PK_JobManager_BigTextTest] PRIMARY KEY CLUSTERED
(
[PKID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data]
) ON [Data] TEXTIMAGE_ON [Data]
XML的SQL插入(工作正常)是......
INSERT INTO JobManager_BigTextTest
(
BigXML
)
VALUES
(
'<ArrayOfTriggerEvent xmlns="http://schemas.datacontract.org/2004/07/RRD.JobManager.AutomatedWorkScheduler.Triggers" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><TriggerEvent i:type="a:ScheduleEvent" xmlns:a="http://schemas.datacontract.org/2004/07/RRD.JobManager.AutomatedWorkScheduler.Triggers.Events"><a:Schedule i:type="b:IncrementalSchedule" xmlns:b="http://schemas.datacontract.org/2004/07/RRD.JobManager.AutomatedWorkScheduler.Schedules"><b:DaysOfMonth xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/><b:DaysOfWeek xmlns:c="http://schemas.datacontract.org/2004/07/System"/><b:Frequency>Daily</b:Frequency><b:LastDayOfMonth>false</b:LastDayOfMonth><b:LastExecuted i:nil="true"/><b:LastModified>2014-08-21T14:40:30.6116736-05:00</b:LastModified><b:EndTime i:nil="true"/><b:Increment>PT5M</b:Increment><b:StartTime>PT0S</b:StartTime></a:Schedule></TriggerEvent><TriggerEvent i:type="a:ScheduleEvent" xmlns:a="http://schemas.datacontract.org/2004/07/RRD.JobManager.AutomatedWorkScheduler.Triggers.Events"><a:Schedule i:type="b:SpecificSchedule" xmlns:b="http://schemas.datacontract.org/2004/07/RRD.JobManager.AutomatedWorkScheduler.Schedules"><b:DaysOfMonth xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><c:int>8</c:int><c:int>11</c:int><c:int>24</c:int></b:DaysOfMonth><b:DaysOfWeek xmlns:c="http://schemas.datacontract.org/2004/07/System"/><b:Frequency>Monthly</b:Frequency><b:LastDayOfMonth>false</b:LastDayOfMonth><b:LastExecuted i:nil="true"/><b:LastModified>2014-08-21T14:40:30.6116736-05:00</b:LastModified><b:StartTimes xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><c:duration>PT11H</c:duration><c:duration>PT11H30M</c:duration><c:duration>PT13H30M</c:duration></b:StartTimes></a:Schedule></TriggerEvent></ArrayOfTriggerEvent>'
)
我用来创建空DataTable的c#代码是......
public static DataTable GetDataTableFromSqlDestination(string tableName)
{
tableName = MakeInjectionProof(tableName); // error if injection discovered
var dataTable = new DataTable();
var query = $"SELECT * FROM [{tableName}] WHERE 1 = 0";
using (var connection = new SqlConnection("my_connection"))
using (var command = new SqlCommand(query, connection))
{
command.CommandTimeout = connection.ConnectionTimeout;
command.CommandType = CommandType.Text;
connection.Open();
var da = new SqlDataAdapter(command);
da.FillSchema(dataTable, SchemaType.Mapped);
connection.Close();
dataTable.PrimaryKey = null; // strip off primary key
dataTable.Columns.Remove("PKID");
}
return dataTable;
}
向DataTable添加一行(或多行)后,执行批量复制的c#方法被调用...
public static void BulkCopyProcessChunk
(
string tableName,
DataTable dataTable,
SqlConnection connection,
SqlTransaction transaction
)
{
using (var sbc = new SqlBulkCopy(connection, SqlBulkCopyOptions.Default, transaction))
{
sbc.BulkCopyTimeout = 0; // Indefinite time out
sbc.DestinationTableName = tableName;
sbc.BatchSize = dataTable.Rows.Count;
foreach (DataColumn r in dataTable.Columns)
{
sbc.ColumnMappings.Add(r.ColumnName, r.ColumnName);
}
sbc.WriteToServer(dataTable); // <--- error occurs here
}
}
XML DataColumn(BigXML)的数据类型为System.String,最大长度为-1。谁能告诉我这里发生了什么?
答案 0 :(得分:1)
错误消息指向第1行,字符29 ...
此时,default-namespace-attribute需要一个字符串文字......您将以double qoutes提供名称空间。所以一切似乎都没问题,但是:
有几个帖子(e.g. this one)指出与SqlBulkCopy
和双qoutes相关的问题。
您可以尝试以下操作:
使用单个qoutes而不是doubled:<ArrayOfTriggerEvent xmlns='Your namespace' NextAttr='SomeVAlue' ...
尝试逃避"
字符。通常,这可以通过将符号加倍(xmlns=""Your namespace""
)或使用转义字符来完成。试试\"
,^"
或反引号(`"
)......