2010-08-27 55 views
0

我應該打電話給專家:D。幫我看看這個漂亮的代碼:)更正此SQL查詢:錯誤「Microsoft Jet數據庫引擎無法找到輸入表或查詢'IF'」

數據庫:

「ID(主鍵)」 | 「標題」
0 | 「title1」
1 | 「title2」
2 | 「title3」
3 | 「TITLE4」


Sub AddRecord(ByVal Table As String, ByVal Columns As String, ByVal Record() As String) 
    Dim SubDir As String = "" 

    Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM " & Table, connectionString) 
    Dim command As OleDbCommand 
    Dim tbCorrectSyntax = "" 

    Using connection As New OleDbConnection(connectionString) 
     Try 
      connection.Open() 
      Dim Commandtxt As String = "" 
      For i = 0 To Record.GetUpperBound(0) 
       If Record(i).IndexOf(",") > 0 Then 
        Dim tmpRec() As String = Nothing 
        Dim cols() As String = Nothing 

        tmpRec = Record(i).Split(",") 
        cols = Columns.Split(",") 

        For j = 0 To tmpRec.GetUpperBound(0) 
         tbCorrectSyntax &= cols(j) & " = '" & tmpRec(j) & "' " & IIf(j = tmpRec.GetUpperBound(0), "", " , ") 
        Next 
       End If 


       Dim txtCorrect As String = IIf(tbCorrectSyntax = "", Columns & " = " & Record(i), tbCorrectSyntax) 

       Commandtxt = "IF OBJECT_ID ('InsertOrUpdateItem', 'P') IS NOT NULL " & _ 
           "DROP PROCEDURE InsertOrUpdateItem " & _ 
          "GO " & _ 
          "CREATE PROCEDURE InsertOrUpdateItem " & _ 
           "AS " & _ 
           "IF (EXISTS (SELECT * FROM " & Table & _ 
              " WHERE " & txtCorrect & "))" & _ 
              " begin " & _ 
               "UPDATE (" & Table & ") " & _ 
               txtCorrect & _ 
               " WHERE " & txtCorrect & " " & _ 
              " End " & _ 
              " else " & _ 
               " begin " & _ 
               " INSERT INTO " & Table & " (" & Columns & ") " & _ 
               " VALUES (" & Record(i) & ")" & _ 
              " End " & _ 
           "End " 

       'Commandtxt = "INSERT INTO " & Table & " (" & Columns & ") VALUES (" & Record(i) & ")" 
       command = New OleDbCommand(Commandtxt, connection) 
       command.CommandType = CommandType.StoredProcedure 
       command.ExecuteNonQuery() 

      Next 


     Catch ex As Exception 
      msgbox(ex.Message) 
     Finally 
      connection.Close() 
     End Try 
    End Using 
End Sub 

Function connectionString() As String 
    Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBdir & ";User Id=admin;Password=;" 
End Function 

第一個程序是一個包裝,以將數據添加到數據庫字段(如果數據doent存在) 但隨着新的數據已經存在更新的行。

輸入:
表=表名
列=將由逗號更新分離colomns的名稱(練習1: 「ID」,實例2: 「ID,標題,...」)
記錄()=字符串數組,代表新的值(多值與逗號分隔)

OK,增加值數據庫之前,我們應該檢查是否有行存在這個值:)
要做到這一點,創建一個存儲過程一種快速處理數據庫的最佳方法。

所以......現在的問題是,在運行時,OLEDB小姐拋出這個錯誤:
Microsoft Jet數據庫引擎無法找到輸入表或查詢「IF」 ....

在此先感謝:D

+0

你移植的SQL Server應用程序來訪問/噴氣? – Constantin 2010-08-27 13:50:14

回答

0

我發現我的問題的解決方案(一點點網研究:))哈哈哈哈我很高興
無論如何,最初的問題是:「如果存在如何更新數據庫中的記錄」 所以我tryed創建並存儲在數據庫中的存儲過程...但是... :)

後來我發現一個有趣的方法:OleDbCommand的的ExecuteScalar

它只需根據輸入的SQL語句返回值:在以下我用過的例子,如果recod存在,它會返回索引(主鍵)。因此,讓我們開始:

Function GetRecordIndex(ByVal Table As String, ByVal Columns As String, ByVal Record As String) As String 
    Dim Commandtxt As String = "" 
    Dim Command As OleDbCommand 
    Dim tbCorrectSyntax As String = "" 
    Dim tbCorrectSyntaxAND As String = "" 

    Using connection As New OleDbConnection(connectionString) 

     Try 
      connection.Open() 
      If Record.IndexOf(",") > 0 Then 
       Dim tmpRec() As String = Nothing 
       Dim cols() As String = Nothing 

       tmpRec = Record.Split(",") 
       cols = Columns.Split(",") 

       For j = 0 To tmpRec.GetUpperBound(0) 
        tbCorrectSyntax &= cols(j) & "='" & tmpRec(j) & "' " & IIf(j = tmpRec.GetUpperBound(0), "", " , ") 
        tbCorrectSyntaxAND &= cols(j) & "='" & tmpRec(j) & "' " & IIf(j = tmpRec.GetUpperBound(0), "", " AND ") 
       Next 
      End If 
      Dim txtCorrect As String = IIf(tbCorrectSyntax = "", Columns & "=" & Record, tbCorrectSyntax) 
      Dim txtCorrectAND As String = IIf(tbCorrectSyntaxAND = "", Columns & "=" & Record, tbCorrectSyntaxAND) 

      Commandtxt = "SELECT * FROM " & Table & " WHERE " & txtCorrectAND 
      Command = New OleDbCommand(Commandtxt, connection) 
      Dim RecordIndex As String = Command.ExecuteScalar 

      Return RecordIndex 

     Catch ex As Exception 
      Return Nothing 
     Finally 
      connection.Close() 
     End Try 
    End Using 
End Function 

與之前一樣,列參數可以是單個數據庫列,也可以是多個用逗號分隔的列。與記錄同樣的事情,代表每列

謝謝里面的數據對你有所幫助
Fadelovesky

0
command.CommandType = CommandType.StoredProcedure 

您聲稱您正在運行存儲過程(CommandText將是現有SProc的名稱)。你實際上給了它一個直接執行的SQL命令。 CommandType應該是Text;

+0

感謝您的回答。我曾嘗試過,但當我把 command.CommandType = CommandType.Text SQL SQL小姐:)拋出一個錯誤:「無效的SQL指令;'刪除','INSERT','程序','選擇'或'更新'預期'。 在這種情況下,你建議我做什麼? – Fadelovesky 2010-08-28 00:19:28

相關問題