由于对象的当前状态,VB.net Oracle 操作无效

发布于 2024-11-01 23:03:21 字数 2016 浏览 5 评论 0原文

好吧,我真的需要找到一种方法来通过字符串来完成此操作,而不是使用更新查询来完成所有操作。

Dim theXMLCode As OracleClob
Dim OracleConnection2 As New OracleConnection()
Dim dr2 As OracleDataReader
Dim holdXML As String = ""

Public Function connectToOracleDB2() As Boolean
    OracleConnection2.ConnectionString = "User Id=" & dbUserId & ";Password=" & dbPassword & ";Data Source=(DESCRIPTION=(ADDRESS_LIST=" & _
                                      "(ADDRESS=(PROTOCOL=TCP)(HOST=" & dbHost & ")(PORT=" & dbPort & ")))" & _
                                     "(CONNECT_DATA=(SERVICE_NAME=" & dbServiceName & ")))"

    Try
        OracleConnection2.Open()
        Return True
    Catch ee As Exception
        OracleConnection2.Close()
        Return False
    End Try
End Function

    Dim strSQL = "UPDATE   CSR.TARGET ces " & _
                 "SET      (STATUS_CODE, COMPLETE_DATE, DATA) = " & _
                    "(SELECT    'ERROR', '', (:XML_DATA) " & _
                    "FROM       CSR.SOURCE C " & _
                    "WHERE      (c.EID = ces.EID) " & _
                    "AND        c.STATUS_CODE = 'ERROR') " & _
                 "WHERE    EXISTS (SELECT 1 " & _
                 "FROM     CSR.SOURCE C " & _
                 "WHERE    (c.EID = ces.EID) " & _
                 "AND      c.STATUS_CODE = 'ERROR')"

        Try
            Dim parmData As New OracleParameter

            With parmData
                .Direction = ParameterDirection.Input
                .OracleDbType = OracleDbType.Clob
                .ParameterName = "XML_DATA"
                .Value = holdXML
            End With

            OracleCommand2.Parameters.Add(parmData)
            OracleCommand2.CommandText = strSQL
            OracleCommand2.ExecuteNonQuery()

但我收到错误:

错误:由于对象的当前状态,操作无效。

在线:

OracleCommand2.ExecuteNonQuery()

任何帮助都会让这个东西正常工作:o)

David

Ok, i am really in need of finding a way to do this via a string to clob instead of using the update query to to do all.

Dim theXMLCode As OracleClob
Dim OracleConnection2 As New OracleConnection()
Dim dr2 As OracleDataReader
Dim holdXML As String = ""

Public Function connectToOracleDB2() As Boolean
    OracleConnection2.ConnectionString = "User Id=" & dbUserId & ";Password=" & dbPassword & ";Data Source=(DESCRIPTION=(ADDRESS_LIST=" & _
                                      "(ADDRESS=(PROTOCOL=TCP)(HOST=" & dbHost & ")(PORT=" & dbPort & ")))" & _
                                     "(CONNECT_DATA=(SERVICE_NAME=" & dbServiceName & ")))"

    Try
        OracleConnection2.Open()
        Return True
    Catch ee As Exception
        OracleConnection2.Close()
        Return False
    End Try
End Function

    Dim strSQL = "UPDATE   CSR.TARGET ces " & _
                 "SET      (STATUS_CODE, COMPLETE_DATE, DATA) = " & _
                    "(SELECT    'ERROR', '', (:XML_DATA) " & _
                    "FROM       CSR.SOURCE C " & _
                    "WHERE      (c.EID = ces.EID) " & _
                    "AND        c.STATUS_CODE = 'ERROR') " & _
                 "WHERE    EXISTS (SELECT 1 " & _
                 "FROM     CSR.SOURCE C " & _
                 "WHERE    (c.EID = ces.EID) " & _
                 "AND      c.STATUS_CODE = 'ERROR')"

        Try
            Dim parmData As New OracleParameter

            With parmData
                .Direction = ParameterDirection.Input
                .OracleDbType = OracleDbType.Clob
                .ParameterName = "XML_DATA"
                .Value = holdXML
            End With

            OracleCommand2.Parameters.Add(parmData)
            OracleCommand2.CommandText = strSQL
            OracleCommand2.ExecuteNonQuery()

But i get the error:

ERROR: Operation is not valid due to the current state of the object.

On the line:

OracleCommand2.ExecuteNonQuery()

Any help would be great to get this thing working :o)

David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窗影残 2024-11-08 23:03:21

由于您找不到创建临时 lob 指针的示例,因此这里是使用以前项目中的 Microsoft Enterprise Library 的 C# 示例。此示例与存储过程交互,但使用 SQL 更新和 BLOB/CLOB 时的方法相同:

internal static void Save(Attachments attachment)
    {
        try
        {
            // Microsoft Enterprise Library does not provide support for Oracle BLOB objects
            // The Microsoft Data Provider for Oracle needs to allocate a BLOB pointer in memory first
            // while running in the context of a database transaction. Once the placeholder is allocated,
            // the byte stream is written to the handler and then passed to Oracle to update the database
            //
            OracleConnection connection = new OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connstring_devl"].ConnectionString);
            connection.Open();
            OracleTransaction transaction = connection.BeginTransaction();
            OracleCommand command = connection.CreateCommand();
            command.Transaction = transaction;
            command.CommandText = "declare xx blob; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;";
            command.Parameters.Add(new OracleParameter("tempblob", OracleType.Blob)).Direction = ParameterDirection.Output;
            command.ExecuteNonQuery();

            OracleLob tempLob = (OracleLob)command.Parameters[0].Value;
            tempLob.BeginBatch(OracleLobOpenMode.ReadWrite);
            if (attachment.FileContent != null)
                tempLob.Write(attachment.FileContent, 0, attachment.FileContent.Length);
            tempLob.EndBatch();

            command.Parameters.Clear();
            command.CommandText = MC_SAVE_ATTACHMENT;
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new OracleParameter("IN_USER_ID", OracleType.VarChar)).Value = attachment.UserID;
            command.Parameters.Add(new OracleParameter("IN_FILE_CONTENT", OracleType.Blob)).Value = tempLob;
            command.Parameters.Add(new OracleParameter("ERROR_DESC", OracleType.VarChar, 4000)).Direction = ParameterDirection.Output;

            command.ExecuteNonQuery();
            transaction.Commit();

            //Check errors and handle it (log, throw exception etc)
            errors = command.Parameters["error_desc"].Value.ToString();
            HandleExceptions.CheckError(errors);
        }
        catch (Exception e)
        {
            string errMsg = e.Message;
            throw;
        }
    }

Since you could not find an example of creating a temporary lob pointer, here is an example in C# using the Microsoft Enterprise Library from a previous project. This example interfaces with a stored procedure, but is the same approach when using a SQL update and BLOB/CLOB:

internal static void Save(Attachments attachment)
    {
        try
        {
            // Microsoft Enterprise Library does not provide support for Oracle BLOB objects
            // The Microsoft Data Provider for Oracle needs to allocate a BLOB pointer in memory first
            // while running in the context of a database transaction. Once the placeholder is allocated,
            // the byte stream is written to the handler and then passed to Oracle to update the database
            //
            OracleConnection connection = new OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connstring_devl"].ConnectionString);
            connection.Open();
            OracleTransaction transaction = connection.BeginTransaction();
            OracleCommand command = connection.CreateCommand();
            command.Transaction = transaction;
            command.CommandText = "declare xx blob; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;";
            command.Parameters.Add(new OracleParameter("tempblob", OracleType.Blob)).Direction = ParameterDirection.Output;
            command.ExecuteNonQuery();

            OracleLob tempLob = (OracleLob)command.Parameters[0].Value;
            tempLob.BeginBatch(OracleLobOpenMode.ReadWrite);
            if (attachment.FileContent != null)
                tempLob.Write(attachment.FileContent, 0, attachment.FileContent.Length);
            tempLob.EndBatch();

            command.Parameters.Clear();
            command.CommandText = MC_SAVE_ATTACHMENT;
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new OracleParameter("IN_USER_ID", OracleType.VarChar)).Value = attachment.UserID;
            command.Parameters.Add(new OracleParameter("IN_FILE_CONTENT", OracleType.Blob)).Value = tempLob;
            command.Parameters.Add(new OracleParameter("ERROR_DESC", OracleType.VarChar, 4000)).Direction = ParameterDirection.Output;

            command.ExecuteNonQuery();
            transaction.Commit();

            //Check errors and handle it (log, throw exception etc)
            errors = command.Parameters["error_desc"].Value.ToString();
            HandleExceptions.CheckError(errors);
        }
        catch (Exception e)
        {
            string errMsg = e.Message;
            throw;
        }
    }
凯凯我们等你回来 2024-11-08 23:03:21
  • 命令对象是否与连接对象关联?
  • 连接是否打开?

一般来说,您会得到如下所示的内容:

Dim conn as new OracleConnection() 'This may have parameters, such as the connection string
OracleCommand2.Connection = conn
conn.Open()
' execute the command
conn.Close()

重要:此代码是凌乱的徒手代码。我不再那么熟悉 VB 语法,但在 C# 中,您可以在 using 语句的声明中实例化 OracleConnection 对象。如果 VB 中没有这样的东西(尽管我怀疑有),那么您可以将其包装在 try/catch/finally 中,以确保连接正确关闭并且对象正确处置。

  • Is the command object associated with a connection object?
  • Is the connection open?

Generally, you'd have something like this:

Dim conn as new OracleConnection() 'This may have parameters, such as the connection string
OracleCommand2.Connection = conn
conn.Open()
' execute the command
conn.Close()

Important: This code is messy free-hand code. I'm not all that familiar with VB syntax anymore, but in C# you'd instantiate the OracleConnection object inside the declaration of a using statement. If there's no such thing in VB (though I suspect there is) then you'd wrap it in a try/catch/finally to make sure the connection is properly closed and the object(s) properly disposed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文