如何使用OracleClientFactory将超过4000个字符插入XmlType?

时间:2011-05-26 16:41:12

标签: c# oracle

我试图将一个大于4000个字符的Xml块插入到Oracle表的XmlType字段中。

最初我的代码是这样的:

DbParameter parameter = new DbParameter;
parameter = clientFactory.CreateParameter(":p_xml_data", DbType.AnsiString, messageToLog.Length, ParameterDirection.Input, messageToLog);

然而,当我开始尝试插入大于4000字节的Xml块时,我得到了:

ORA-01461: can bind a LONG value only for insert into a LONG column

这是与此问题2508987/insert-xml-with-more-than-4000-characters-into-a-oracle-xmltype-column相同的问题,但是我没有DbType.Clob作为选项(它不存在)。

接下来我尝试将类型更改为DbType.Object,希望它只是将其转换为它需要的任何内容,但我收到此消息:

Cannot bind type System.String as Blob

然后我尝试使用DbType.XML,我修改了我的代码,将messageToLog移动到SqlXml对象中:

SqlXml sx;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(messageToLog);
using (XmlNodeReader xnr = new XmlNodeReader(xmlDoc))
{
    sx = new SqlXml(xnr);
}

并相应更改了参数:

parameter = providerFactory.CreateParameter(":p_xml_data", DbType.Xml, messageToLog.Length, ParameterDirection.Input, sx);

现在我明白了:

Value is not valid for DbType: Xml

真的,我只想在我的专栏中存储更大的XML块。

2 个答案:

答案 0 :(得分:2)

您应该是ODP.NET库的Oracle Docs,特别是用于设置XMLType信息。 OracleXmlType类及其用法也分别为described。我自己并不是真正的C#开发人员,所以我无法给你准确的代码。 This is what I found四处寻找,但我自己没有尝试过:

OracleCommand orainscmd = new OracleCommand("INSERT INTO employees(empinfo) " +
" VALUES (:empinfoxml)", con);
orainscmd.Parameters.Add(":empinfoxml", OracleDbType.XmlType);
OracleXmlType xmlinfo = new OracleXmlType(con,doc);
orainscmd.Parameters[0].Value=xmlinfo;
orainscmd.ExecuteNonQuery();

答案 1 :(得分:0)

将它绑定如下:.OracleDbType = OracleDbType.XmlType;

.DBType = DbType.Xml不起作用,并且会因为枚举值超出范围而抛出运行时错误。

见下文:

    public void insertSimRun(OracleConnection conn, SliceDataExchange.ServiceReference1.SimulationRun simRun)
    {
        string sqlInsert = "INSERT INTO slice_sim (runid, first_int_start, simulation_run) ";
        sqlInsert += "values (:p_runid, :p_interval_start, :p_simxml)";

        OracleCommand cmdInsertSR = new OracleCommand();
        cmdInsertSR.CommandText = sqlInsert;
        cmdInsertSR.Connection = conn;

        OracleParameter runID = new OracleParameter();
        runID.DbType = DbType.Int32;
        runID.Value = simRun.RunId;
        runID.ParameterName = "p_runid";

        OracleParameter first_interval_start = new OracleParameter();
        first_interval_start.DbType = DbType.DateTime;
        first_interval_start.Value = simRun.FirstIntervalStartUtc;
        first_interval_start.ParameterName = "p_interval_start";

        var serializer = new XmlSerializer(typeof(SliceDataExchange.ServiceReference1.SimulationRun));
        StringWriter writer = new StringWriter();
        //System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();

        serializer.Serialize(writer,simRun);

        //xdoc.LoadXml(writer.ToString());

        OracleParameter simRunXML = new OracleParameter();
        simRunXML.DbType = DbType.String;
        simRunXML.ParameterName = "p_simxml";
        simRunXML.Value = writer.ToString();
        simRunXML.OracleDbType = OracleDbType.XmlType;


        cmdInsertSR.Parameters.Add(runID);
        cmdInsertSR.Parameters.Add(first_interval_start);
        cmdInsertSR.Parameters.Add(simRunXML);

        cmdInsertSR.ExecuteNonQuery();

        cmdInsertSR.Dispose();
    }