I want to pass a paramter of type XML to a param in a TVP. I tried the below code, which does not work. I am unable to figure out if there is a way to pass an xml data to a TVP in sql server.
sql
CREATE TABLE XmlSample(Id INT, Profile XML);
CREATE TYPE [SampleProfile_DataTYPE] AS TABLE
(
Id INT,
Profile XML
);
CREATE PROCEDURE XmlTvpSprocTest_Save
@SamplesTvp [SampleProfile_DataTYPE] READONLY
AS
BEGIN
INSERT INTO XmlSample (Id, Profile)
SELECT DeviceID, Profile
FROM @SamplesTvp
END
Applicatin side code in C#
public void SaveXmlTestData()
{
string xdata = @"<?xml version=""1.0"" encoding=""ISO - 8859 - 1""?><note><to>Rahul</to></note>";
var samples= GetSamplesTvp(1, 1, XElement.Parse(xdata));
using (DbCommand cmd = database.GetStoredProcCommand("XmlTvpSprocTest_Save"))
{
var param = new SqlParameter("@SamplesTvp ", samples) { SqlDbType = SqlDbType.Structured };
cmd.Parameters.Add(param);
database.ExecuteNonQuery(cmd);
}
}
private DataTable GetSamplesTvp(int deviceId, XmlDocument xmldata)
{
var table= new DataTable();
table.Columns.Add("Id", typeof(int));
table.Columns.Add("Profile", typeof(XElement));
table.Rows.Add(deviceId, xmldata);
return table;
}
I get the exception The type of column is not supported. The xml parameter has to be included in a TVP (Profile) along with other parameters. part of the reason is that, I will have to save a list of xml profiles along with their corresponding Ids, did someone come across anything similar?