2012-02-02 85 views
1

我在SQL Server 2005中有一个列的数据类型为xml的表。我创建了一个存储过程以将值插入到该列中。通过存储过程插入XML数据时出错

但在C#代码从调用SP时,它给我未能参数值从一个的XDocument转换为字符串错误

任何人都可以帮忙吗?

这是存储过程,我创建:

Create Procedure [dbo].[TestReportRepository_Save] 
@ReportExecutionTime datetime, 
@ReportFile xml, 

as 
begin 

insert into TestReportRepository(ReportExecutionTime,ReportFile) values(@ReportExecutionTime,@ReportFile) 

end 

的C#代码是

DbParameter dbParam1 = dac.Parameter("@ReportExecutionTime", ReportExecutionTime, DbType.DateTime, ParameterDirection.Input); 
DbParameter dbParam2 = dac.Parameter("@ReportFile", xmlDoc.Document, DbType.Xml, ParameterDirection.Input); 

DbParameter[] dbParamColl = new DbParameter[] { dbParam1, dbParam2 }; 
long reportID = dac.Save("TestReportRepository_save", dbParamColl); 

请帮我鉴定一下我做错了。

+0

如果您发布的代码,XML或数据样本,**请**突出显示文本编辑器的线,然后点击“代码(编辑器)工具栏上的“样本”按钮(“{}”),以精确地格式化和语法突出显示它! – 2012-02-02 11:27:07

+3

在你的C#代码中,你提供了参数的值?我没有看到。基本上--XML类型的SQL Server是一个荣耀的字符串 - 它不能直接处理'XDocument' - 你必须将'XDocument'序列化为一个字符串以将它传递给SQL Server(例如,使用''。 ToString()'方法) – 2012-02-02 11:33:28

+0

谢谢@marc_s:在发布任何新问题时,我会记住你的建议。也感谢解决方案。 – 2012-02-02 13:22:08

回答

3

传递此参数时 - 不传递XDocument对象,而是传递其字符串表示形式。

对于XmlDocument的是XmlDocument.OuterXml,找到一个对的XDocument,并将它传递

+0

@mark_s对不起,我在写答案的时候没有注意到你的评论,如果你确定它是代表嫖娼 - 我会删除答案 – 2012-02-02 11:42:22

+0

不用担心:-)我只是不想把它扩展到“真实”的答案 - 你做到了 - 你值得信赖你的努力! – 2012-02-02 13:23:07

+0

@OlegDok有用的信息从你身边。谢谢您的帮助。 – 2012-02-02 13:23:55