简体   繁体   English

如何将 XML 数据作为参数传递给从 C# 到 Sql 服务器的 TVP 参数

[英]How to pass XML data as a parameter to a TVP param from C# to Sql server

I want to pass a paramter of type XML to a param in a TVP.我想将 XML 类型的参数传递给 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.我无法弄清楚是否有办法将 xml 数据传递给 sql server 中的 TVP。

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# 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 type of column is not supported The xml parameter has to be included in a TVP (Profile) along with other parameters. xml 参数必须与其他参数一起包含在 TVP(配置文件)中。 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?部分原因是,我将不得不保存一个 xml 配置文件列表及其相应的 ID,有人遇到过类似的事情吗?

passing the data as string actually worked将数据作为string传递实际上有效

 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虽然,如果我将注释掉的 xml 添加到 xmlData (Name cannot start with '.' character) ,我确实会遇到异常。这是另一个问题,但问题的核心通过简单地将参数作为字符串从 c# 传递并保持sql 中的 tvp 参数作为 Xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM