【问题标题】:Correct this SQL Query: error "Microsoft Jet database engine cannot find the input table or query 'IF' "更正此 SQL 查询:错误“Microsoft Jet 数据库引擎找不到输入表或查询 'IF'”
【发布时间】:2023-04-06 08:25:01
【问题描述】:

我应该说嗨专家:D。帮我写这段漂亮的代码:)

数据库:

"ID(主键)" | “标题”
0 | “标题1”
1 | “标题2”
2 | “标题3”
3 | “标题4”


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

第一个过程是将数据添加到数据库字段的包装器(如果数据不存在) 但是用已经存在的新数据更新它。

输入:
表 = 表名
Columns = 将要更新的列的名称,以逗号分隔(Ex1:“ID”,Ex2:“ID,Title,...”)
Record() = 表示新值的字符串数组(多个值用逗号分隔)

好的,在向数据库添加值之前,我们应该检查是否存在具有该值的行:)
为此,创建存储过程是快速处理数据库的最佳方式。

所以...现在的问题是,在运行时,Miss OleDB 抛出了这个错误:
Microsoft Jet 数据库引擎找不到输入表或查询 'IF' ....

提前致谢:D

【问题讨论】:

  • 您是否正在将 SQL Server 应用程序移植到 Access/Jet?

标签: sql vb.net oledb command executenonquery


【解决方案1】:
 command.CommandType = CommandType.StoredProcedure 

您声称您正在运行存储过程(CommandText 将是现有 SProc 的名称)。您实际上是在给它一个要直接执行的 SQL 命令。 CommandType 应该是 Text;

【讨论】:

  • 感谢您的回答。我以前试过,但是当我把 command.CommandType = CommandType.Text Miss SQL :) 抛出一个错误:“无效的 SQL 指令;'DELETE','INSERT','PROCEDURE','SELECT'或'UPDATE'预期”。在这种情况下,你建议我怎么做?
【解决方案2】:

我找到了解决问题的方法(通过一些网络研究:))哈哈哈我很高兴
无论如何,最初的问题是:“如何更新数据库中的记录(如果存在)” 所以我尝试在数据库中创建和存储一个存储过程......但是...... :)

然后我发现了一个有趣的方法:OleDBcommand 类的 ExecuteScalar

它只是根据 Sql 输入返回一个值:在我使用的以下示例中,如果记录存在,它返回索引(主键)。那么让我们开始吧:

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

和以前一样,Columns 参数可以是单个数据库列,也可以是用逗号分隔的多个列。与表示每列内数据的 Record 相同

感谢您的帮助
法德洛夫斯基

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多