值分配错误-值不在预期范围内

时间:2019-05-07 21:21:46

标签: c# .net xml oracle xmltype

我正在尝试将值发送给oracle参数,然后再将其发送到数据库,但是这种特定的XMLTYPE正在抛出
System.ArgumentException:'值不在预期范围内。'

我尝试了各种建议,但大多数建议都涉及重新启动笔记本电脑或Visual Studio设置等,我认为这与XType的XMLType有关。但是,类似的代码也可以在其他解决方案中使用。

我的C#代码如下:

public static string SaveMyData(int myId, XDocument myData)
        {            
            try
            {
                OracleParameter[] parameters = new OracleParameter[3];
                parameters[0] = new OracleParameter() 
                {
                    ParameterName = "P_ID",
                    Value = myId,
                    OracleDbType = OracleDbType.Int16,
                    Direction = ParameterDirection.Input
                };             

                parameters[1] = new OracleParameter()
                {
                    ParameterName = "P_DATA",
                    Value = myData,
                    OracleDbType = OracleDbType.XmlType,
                    Direction = ParameterDirection.Input
                };

                parameters[2] = new OracleParameter()
                {
                    ParameterName = "P_RESULT",
                    OracleDbType = OracleDbType.Varchar2,
                    Direction = ParameterDirection.Output
                };

                string myresult = DataAccess.DataContext.GetInstance().Data.ExecuteProcWithParameter(UPDATE_MY_DATA, parameters)["P_RESULT"].ToString();

                return myresult;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

myData的值为:

<players>
  <player>
    <id>10101</id>
    <name>Ricardo Ferreira Rodrigues</name>
    <shirtnumber>1</shirtnumber>
    <position>Guarda Redes</position>
    <realteam>5</realteam>
  </player>
  <player>
    <id>10103</id>
    <name>Antonio Manuel</name>
    <shirtnumber>2</shirtnumber>
    <position>Defesa</position>
    <realteam>5</realteam>
  </player>
</players>

我什至尝试将myData更改为XmlDocument,但这同样行不通。对可能出什么问题有任何想法吗?

2 个答案:

答案 0 :(得分:1)

您需要一个OracleXmlType实例而不是XDocument并将XML作为字符串参数(mydata.ToString())传递

答案 1 :(得分:0)

你能尝试这个吗?

    XMLString = "<players>
  <player>
    <id>10101</id>
    <name>Ricardo Ferreira Rodrigues</name>
    <shirtnumber>1</shirtnumber>
    <position>Guarda Redes</position>
    <realteam>5</realteam>
  </player>
  <player>
    <id>10103</id>
    <name>Antonio Manuel</name>
    <shirtnumber>2</shirtnumber>
    <position>Defesa</position>
    <realteam>5</realteam>
  </player>
</players>";


parameters[1] = new OracleParameter()
            {
                ParameterName = "P_DATA",
                Value = XMLString,
                OracleDbType = OracleDbType.XmlType,
                Direction = ParameterDirection.Input
            };