C#SerializeTOXML将其转换为SQL命令发出的字符串 [英] C# SerializeTOXML converting it to string for SQL command issue

查看:74
本文介绍了C#SerializeTOXML将其转换为SQL命令发出的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,对不起,标题很复杂,但是我在SSIS脚本中运行C#应用程序,但是很奇怪,如果我在脚本C#代码的任何位置插入断点,则可以正常工作,但是如果删除所有的断点,让它运行,会导致 此错误消息:

 SSIS包"Member.dtsx"开始.
错误:脚本任务1:System.Reflection.TargetInvocationException处的0x1:调用的目标引发了异常. ---> System.InvalidOperationException:生成XML文档时出错. ---> System.TypeInitializationException:'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1'的类型初始化程序引发了异常. ---> System.NullReferenceException:对象引用未设置为对象的实例.
   在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1..cctor()
   ---内部异常堆栈跟踪的结尾---
   在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1..ctor()
   在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializerContract.get_Writer()
   在System.Xml.Serialization.TempAssembly.InvokeWriter处(XmlMapping映射,XmlWriter xmlWriter,对象o,XmlSerializerNamespaces命名空间,字符串encodingStyle,字符串id)
   在System.Xml.Serialization.XmlSerializer.Serialize处(XmlWriter xmlWriter,对象o,XmlSerializerNamespaces命名空间,字符串encodingStyle,字符串id)
   ---内部异常堆栈跟踪的结尾---
   在System.Xml.Serialization.XmlSerializer.Serialize处(XmlWriter xmlWriter,对象o,XmlSerializerNamespaces命名空间,字符串encodingStyle,字符串id)
   在System.Xml.Serialization.XmlSerializer.Serialize中(TextWriter textWriter,对象o,XmlSerializerNamespaces命名空间)
   在System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter,Object o)
   在ST_e64f8f5fd7d74450ad8da786d041f01a.csproj.ScriptMain.SerializeToXML(清单1 iMbrElig)
   在ST_e64f8f5fd7d74450ad8da786d041f01a.csproj.ScriptMain.Main()
   ---内部异常堆栈跟踪的结尾---
   在System.RuntimeMethodHandle._InvokeMethodFast处(对象目标,Object []参数,SignatureStruct&sig,MethodAttributes methodAttributes,RuntimeTypeHandle typeOwner)
   在System.RuntimeMethodHandle.InvokeMethodFast处(对象目标,对象[]参数,签名sig,MethodAttributes,methodAttributes,RuntimeTypeHandle typeOwner)
   在System.Reflection.RuntimeMethodInfo.Invoke处(对象obj,BindingFlags invokeAttr,活页夹活页夹,Object []参数,CultureInfo文化,布尔skipVisibilityChecks)
   在System.Reflection.RuntimeMethodInfo.Invoke处(对象obj,BindingFlags invokeAttr,活页夹活页夹,Object []参数,CultureInfo文化)
   在System.RuntimeType.InvokeMember处(字符串名称,BindingFlags bindingFlags,活页夹绑定程序,对象目标,Object []提供的Args,ParameterModifier []修饰符,CultureInfo文化,String [] namedParams)
   在System.Type.InvokeMember处(字符串名称,BindingFlags invokeAttr,活页夹活页夹,对象目标,Object [] args,CultureInfo文化)
   在Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
任务失败:脚本任务1
警告:成员:SSIS警告代码DTS_W_MAXIMUMERRORCOUNTREACHED为0x80019002.执行方法成功,但是引发的错误数(1)达到了允许的最大值(1);导致失败.当错误数量达到MaximumErrorCount中指定的数量时,就会发生这种情况.更改MaximumErrorCount或修复错误.
SSIS包"Member.dtsx"完成:失败.

通过查看代码,我可能是错的,因为我只是一个DBA而不是真正的开发人员,看来它试图将iEV的SerializeToXML列表转换为字符串,然后使其等于新值的sql参数?

下面是我追踪到的确切行,是其原因,任何帮助将不胜感激:

 sqlCmd.Parameters ["@ EligData"].Value = Convert.ToString(SerializeToXML(iEV)); 

解决方案

请在SSIS论坛中发布与SSIS相关的问题.

您遇到的特定错误是空引用异常.设置或不设置断点都不会对此产生影响.我怀疑问题在于您获取的数据.由于它可以与断点一起工作,我猜是数据根本没有被发送出去 然而.尝试初始化用于序列化数据的类型时发生错误.没有任何更多信息,很难说出数据出了什么问题. SerializeToXML不是SSIS提供的代码.基于调用栈进行定义 在您的脚本任务中.该代码可能使用构造函数调用(新)创建XmlSerializer.这触发了用于进行序列化的动态程序集的生成.

该调用栈表明它在Serialize调用本身中失败,因此创建了序列化程序,这意味着类型本身可能很好.但是在序列化过程中,它遇到了自己不喜欢的数据,并引发了异常.这似乎为空 当它没想到一个.我建议您将这段代码包装在try catch中,并记录返回iEV值所需的信息,以便您可以仔细查看它.

尝试
{
   sqlCmd.Parameters ["@ EligData"].Value =转换...
} catch(异常e)
{
   Dts.Events.FireError(0,"Serialization error",e.Message);
   //记录有关iEV的任何数据,以便您查看正在发送的数据
   Dts.TaskResult =(int)ScriptResults.Failure;
   返回;
}; 

迈克尔·泰勒
http://www.michaeltaylorp3.net


Hello, sorry for the complicated title, however I am running a C# application in SSIS script, however it is weird, if i insert a breakpoint anywhere in the script C# code, it works fine, however if i remove all break points and let it run, it will cause this error message:

SSIS package "Member.dtsx" starting.
Error: 0x1 at Script Task 1: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.TypeInitializationException: The type initializer for 'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1..ctor()
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializerContract.get_Writer()
   at System.Xml.Serialization.TempAssembly.InvokeWriter(XmlMapping mapping, XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
   at ST_e64f8f5fd7d74450ad8da786d041f01a.csproj.ScriptMain.SerializeToXML(List`1 iMbrElig)
   at ST_e64f8f5fd7d74450ad8da786d041f01a.csproj.ScriptMain.Main()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
Task failed: Script Task 1
Warning: 0x80019002 at Member: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.  The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
SSIS package "Member.dtsx" finished: Failure.

from looking at the code, and i can be wrong as i am just a DBA and not really a developer, it seems its trying to convert the SerializeToXML List of iEV to a string, then make it equal to be the new value of sql parameters?

below is the exact line i have traced it to be the cause of it, any help would be greatly appreciated:

sqlCmd.Parameters["@EligData"].Value = Convert.ToString(SerializeToXML(iEV));

解决方案

Please post questions related to SSIS in the SSIS forum.

The particular error you're getting is a null reference exception. Setting a breakpoint or not wouldn't impact that. I suspect the issue is the data you're getting. Since it works with a breakpoint my guess is that the data simply hasn't been sent through yet. The error occurred while trying to initialize the type used to serialize the data. Without any further information it is hard to tell what is wrong with the data. SerializeToXML isn't code that is provided by SSIS. Based upon the callstack it is defined in your script task. That code is likely creating an XmlSerializer using the constructor call (new). This triggers the generation of a dynamic assembly that is used to do the serialization. 

The callstack indicates it fails in the Serialize call itself so the serializer was created which means the type itself is probably fine. But during serialization it ran into data that it didn't like and threw an exception. This would appear to be a null when it didn't expect one. I'd recommend that you wrap this code in a try catch and log the information needed to get back to the iEV value so you can take a closer look at it.

try
{
   sqlCmd.Parameters["@EligData"].Value = Convert...
} catch (Exception e)
{
   Dts.Events.FireError(0, "Serialization error", e.Message);
   //Log any data needed about iEV so you can look at the data being sent
   Dts.TaskResult = (int)ScriptResults.Failure;
   return;
};

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于C#SerializeTOXML将其转换为SQL命令发出的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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