执行Oracle存储过程时出错

时间:2017-03-01 10:28:48

标签: vb.net oracle insert procedure

我有一个小项目,我尝试通过vb.net接口的存储过程将一些值插入Oracle

以下是代码:

Oracle代码

CREATE SEQUENCE my_doc_seq INCREMENT BY 1 START WITH 1 MAXVALUE 999999 MINVALUE 1;
/
CREATE TABLE MY_DOC 
   (ID NUMBER PRIMARY KEY, 
    FILE_NAME VARCHAR2(255 BYTE), 
    UPLOAD_DATE VARCHAR2(10 BYTE), 
    FILESIZE VARCHAR2(20 BYTE), 
    FILETYPE VARCHAR2(5 BYTE), 
    CONTENT BFILE, 
    CREATION_DATE DATE DEFAULT SYSDATE NOT NULL ENABLE, 
    MODIFICATION_DATE DATE, 
    ACCESSED_DATE DATE)
/
CREATE INDEX MY_DOC_IDX ON MY_DOC (CONTENT) INDEXTYPE IS CTXSYS.CONTEXT ;
/
CREATE OR REPLACE DIRECTORY DOCUMENTS AS 'D:\docs';
/
create or replace PROCEDURE put_file
 (
  p_file_name         IN my_doc.file_name%TYPE,
  p_upload_date       IN my_doc.upload_date%TYPE,
  p_filesize          IN my_doc.filesize%TYPE,
  p_filetype          IN my_doc.filetype%TYPE,
  p_creation_date     IN my_doc.creation_date%TYPE, 
  p_modification_date IN my_doc.modification_date%TYPE, 
  p_accessed_date     IN my_doc.accessed_date%TYPE
) AS
 BEGIN
   INSERT INTO my_doc (id, file_name, upload_date, filesize, filetype, content, creation_date, modification_date, accessed_date)
   VALUES (my_doc_seq.NEXTVAL, p_file_name, p_upload_date, p_filesize, p_filetype, BFILENAME('DOCUMENTS',p_file_name), p_creation_date, p_modification_date, p_accessed_date);
   COMMIT;

END;

VB.NET代码

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Try
        Dim strFolder As String = "D:\docs\"
        Dim connectionString As String = "Data Source=orcl;Persist Security Info=True;User ID=my_user;Password=my_password"
        Dim connection As New OracleConnection(connectionString)
        Dim fso As New Scripting.FileSystemObject()
        Dim oFolder As Scripting.Folder = fso.GetFolder(strFolder)

        connection.Open()

        For Each oFile As String In Directory.GetFiles(strFolder)
        Dim strCmd As String = "licenta.put_file('" & New FileInfo(oFile).Name & "', " & _
        "SYSDATE, '" & _
        New FileInfo(oFile).Length & "', '" & _
        New FileInfo(oFile).Extension & "', '" & _
        New FileInfo(oFile).CreationTime.ToString("dd-MMM-yyyy") & "', '" & _
        New FileInfo(oFile).LastWriteTime.ToString("dd-MMM-yyyy") & "', '" & _
        New FileInfo(oFile).LastAccessTime.ToString("dd-MMM-yyyy") & "')"

        Dim cmd As New OracleCommand
        cmd.Connection = connection

        cmd.CommandText = strCmd
        cmd.CommandType = CommandType.Text

        Dim temp As Integer = cmd.ExecuteNonQuery()

        If temp > 0 Then
            MessageBox.Show("File name " & New FileInfo(oFile).Name & " inserted!")
        Else
            MessageBox.Show("An error occurred, file name " & New FileInfo(oFile).Name & " not inserted!")
        End If

        MessageBox.Show("strCmd= " & strCmd)
        Next

        connection.Close()
        strFolder = Nothing
        Catch

        End Try
    End Sub

当我执行VB代码时,我在CATCH上收到以下错误,但是我在“strCmd”中生成的字符串在我直接放在Oracle中时效果很好。

  

ORA-00900:无效的SQL语句

有人可以帮我调试吗?

非常感谢, Mikcutu。

注意:我也尝试过参数化查询,代码如下:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Try
        Dim strFolder As String = "D:\docs\"
        Dim connectionString As String = "Data Source=orcl;Persist Security Info=True;User ID=my_user;Password=my_password"
        Dim connection As New OracleConnection(connectionString)
        Dim fso As New Scripting.FileSystemObject()
        Dim oFolder As Scripting.Folder = fso.GetFolder(strFolder)

        connection.Open()

        For Each oFile As String In Directory.GetFiles(strFolder)

            Dim cmd As New OracleCommand
            cmd.Connection = connection

            cmd.Parameters.Add(New FileInfo(oFile).Name, OracleDbType.Varchar2, 255).Direction = ParameterDirection.Input
            cmd.Parameters.Add(Date.Now().ToString("dd-MMM-yyyy"), OracleDbType.Date).Direction = ParameterDirection.Input
            cmd.Parameters.Add(ToString(New FileInfo(oFile).Length), OracleDbType.Int64).Direction = ParameterDirection.Input
            cmd.Parameters.Add(New FileInfo(oFile).Extension, OracleDbType.Varchar2, 5).Direction = ParameterDirection.Input
            cmd.Parameters.Add(New FileInfo(oFile).CreationTime.ToString("dd-MMM-yyyy"), OracleDbType.Date).Direction = ParameterDirection.Input
            cmd.Parameters.Add(New FileInfo(oFile).LastWriteTime.ToString("dd-MMM-yyyy"), OracleDbType.Date).Direction = ParameterDirection.Input
            cmd.Parameters.Add(New FileInfo(oFile).LastAccessTime.ToString("dd-MMM-yyyy"), OracleDbType.Date).Direction = ParameterDirection.Input

            cmd.CommandText = "put_file"
            cmd.CommandType = CommandType.StoredProcedure
            Dim temp As Integer = cmd.ExecuteNonQuery()

            If temp > 0 Then
                MessageBox.Show("File name " & New FileInfo(oFile).Name & " inserted!")
            Else
                MessageBox.Show("An error occurred, file name " & New FileInfo(oFile).Name & " not inserted!")
            End If
          connection.Close()
            strFolder = Nothing
    Catch
End Try
End Sub

1 个答案:

答案 0 :(得分:0)

最后,我找到了解决方案。即使我尝试了参数化查询,我也没有正确使用它(不提供参数值。我只是定义了它们)。 这是解决方案:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Try
        Dim strFolder As String = "D:\docs\"
        Dim connectionString As String = "Data Source=orcl;Persist Security Info=True;User ID=my_user;Password=my_password"
        Dim connection As New OracleConnection(connectionString)
        Dim fso As New Scripting.FileSystemObject()
        Dim oFolder As Scripting.Folder = fso.GetFolder(strFolder)

        connection.Open()

        For Each oFile As String In Directory.GetFiles(strFolder)

            Dim cmd As New OracleCommand
            cmd.Connection = connection

            cmd.Parameters.Add("p_file_name", OracleDbType.Varchar2, 255).Direction = ParameterDirection.Input
            cmd.Parameters("p_file_name").Value = New FileInfo(oFile).Name

            cmd.Parameters.Add("p_upload_date", OracleDbType.Date).Direction = ParameterDirection.Input
            cmd.Parameters("p_upload_date").Value = Date.Now()

            cmd.Parameters.Add("p_filesize", OracleDbType.Int64).Direction = ParameterDirection.Input
            cmd.Parameters("p_filesize").Value = New FileInfo(oFile).Length

            cmd.Parameters.Add("p_filetype", OracleDbType.Varchar2, 5).Direction = ParameterDirection.Input
            cmd.Parameters("p_filetype").Value = New FileInfo(oFile).Extension

            cmd.Parameters.Add("p_creation_date", OracleDbType.Date).Direction = ParameterDirection.Input
            cmd.Parameters("p_creation_date").Value = New FileInfo(oFile).CreationTime

            cmd.Parameters.Add("p_modification_date", OracleDbType.Date).Direction = ParameterDirection.Input
            cmd.Parameters("p_modification_date").Value = New FileInfo(oFile).LastWriteTime

            cmd.Parameters.Add("p_accessed_date", OracleDbType.Date).Direction = ParameterDirection.Input
            cmd.Parameters("p_accessed_date").Value = New FileInfo(oFile).LastAccessTime

            cmd.CommandText = "put_file"
            cmd.CommandType = CommandType.StoredProcedure
            Dim temp As Integer = cmd.ExecuteNonQuery()

            If temp > 0 Then
                MessageBox.Show("File name " & New FileInfo(oFile).Name & " inserted!")
            Else
                MessageBox.Show("An error occurred, file name " & New FileInfo(oFile).Name & " not inserted!")
            End If

            connection.Close()
            strFolder = Nothing
        Next
    Catch
    End Try
End Sub