Error al insertar datos XML a través del procedimiento almacenado

I have a table with the column's datatype as xml in SQL Server 2005. I created a stored procedure to insert values into that column.

But when calling the SP from code in C#, its giving me an error

Failed to convert parameter value from a XDocument to a String.

¿Alguien puede ayudarme con esto?

This is the stored procedure I created:

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

as
begin

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

end

El código C # es

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);

Please help me identify what I am doing wrong.

preguntado el 02 de febrero de 12 a las 11:02

Si publica muestras de código, XML o datos, POR FAVOR resalte esas líneas en el editor de texto y haga clic en el botón "ejemplos de código" ( { } ) en la barra de herramientas del editor para formatearlo y resaltarlo con sintaxis. -

Where in your C# code are you supplying the values for the parameters?? I don't see that. Basically - the XML type of SQL Server is a glorified string - it cannot deal with an XDocument directly - you'll have to "serialize" that XDocument into a string to pass it to SQL Server (e.g. usign the .ToString() método) -

Thanks @marc_s: I will keep your suggestion in mind while posting any new Question. Also Thanks for the solution it worked out. -

1 Respuestas

When passing this parameter - pass not XDocument object, but its string representation.

For XmlDocument it is XmlDocument.OuterXml, find one for XDocument and pass it

Respondido 02 Feb 12, 15:02

@mark_s Sorry, I've not noticed your comment when wrote the answer, if you decide that it is a rep whoring - i'll delete the answer - Oleg Dok

No worries :-) I just didn't feel like expanding this to a "real" answer - you did - you deserve the credit for your effort! - marc_s

@OlegDok Useful information from your side. Thanks for the help. - Sumit Gupta

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.