0

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?

jarlh
  • 42,561
  • 8
  • 45
  • 63
Rahul
  • 51
  • 1
  • 4

1 Answers1

0

passing the data as string actually worked

 public void SaveXmlTestData_WithString()
        {
            //<?xml version=""1.0"" encoding=""UTF-8""?><!DOCTYPE plist PUBLIC"">
            string xdata = @"<note><to>profile</to></note>";
            var samples= GetSamplesTvp(1, xdata);
            using (DbCommand cmd = database.GetStoredProcCommand("XmlTvpSprocTest"))
            {
                var param1 = new SqlParameter("@SamplesTvp ", samples) { SqlDbType = SqlDbType.Structured };
                cmd.Parameters.Add(param1);
                database.ExecuteNonQuery(cmd);
            }
        }

private List<SqlDataRecord> GetSamplesTvp(int Id, string xmldata)
        {
            List<SqlDataRecord> tvprecord = new List<SqlDataRecord>();
            var column1 = new SqlMetaData("Id", SqlDbType.Int);
            var column2 = new SqlMetaData("Profiles", SqlDbType.Xml);
            var data = new SqlMetaData[] { column1, column2 };
            var sqlrecord = new SqlDataRecord(data);
            sqlrecord.SetValue(0, Id);
            sqlrecord.SetValue(1, xmldata);
            tvprecord.Add(sqlrecord);
            return tvprecord;
        }

Although, I do get an exception if I add the commented out xml to the xmlData (Name cannot start with '.' character) That's another issue but the core of the problem got solved by simply passing the param as string from c# and keeping the tvp param in sql as Xml

Rahul
  • 51
  • 1
  • 4