2017-03-01 209 views
0

我有一個小項目,我嘗試通過存儲過程從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代碼,我接收地e在CATCH上出現以下錯誤,但我在「strCmd」中生成的字符串在我直接將其放入Oracle中時工作得很好。

ORA-00900: invalid SQL statement

任何人都可以幫助我進行調試嗎?

非常感謝, 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 
+2

什麼錯誤,它發生在哪裏 - 我不只是在你的「試試看」? –

+1

除了Alex M對於更多信息的請求外,數據庫服務器可以看到D:\ docs目錄嗎? (也就是說,您不是試圖訪問數據庫服務器未映射的客戶機上的目錄) – Boneist

+0

[使用參數化查詢。](http://stackoverflow.com/q/811481/1509264) – MT0

回答

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