更正此 SQL 查询:错误“Microsoft Jet 数据库引擎找不到输入表或查询“IF”; ”

发布于 2024-09-16 22:18:44 字数 3573 浏览 1 评论 0原文

我应该向专家们问好:D。帮我写这个漂亮的代码:)

数据库:

“ID(主键)”| “标题”
0 | “标题1”
1 | “标题2”
2 | “标题3”
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

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

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

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

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

提前致谢:D

i should say hi experts :D . Help me with this pretty code :)

The database:

"ID (Primary key)" | "Title"
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

The first procedure is a wrapper to add data to database fields (if the data doent exist)
But update it the row with new data already exist.

Input:
Table = TableName
Columns = name of colomns that will be updated separated by comma (Ex1: "ID" , Ex2: "ID,Title,...")
Record() = string array that represent the new values( multiple values are separated with comma)

OK, before adding values to database, we should check if a row exists with this values :)
TO do this, creating a Stored Procedure is a best way to deal with the database fastly.

So... The problem now is, at the runtime, Miss OleDB throw this error:
Microsoft Jet database engine cannot find the input table or query 'IF' ....

Thanks in advance :D

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

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

发布评论

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

评论(2

近箐 2024-09-23 22:18:44
 command.CommandType = CommandType.StoredProcedure 

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

 command.CommandType = CommandType.StoredProcedure 

You are claiming you are running a stored proc (CommandText would be the name of an existing SProc). What you are actually giving it an SQL command to be executed directly. The CommandType should be Text;

原谅过去的我 2024-09-23 22:18:44

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

然后我发现了一个有趣的方法: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

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

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

I found a solution for my problem (with a little net research :) ) hahahaha i m happy
anyway, the initial question was: 'How to update a record in database if it exists'
So i tryed to create and store a Stored procedure in the database... but... :)

Then i found an interesting Method: the ExecuteScalar of OleDBcommand Class

It simply return a value according to Sql input : in the following example that i used, it return the index (primary key) if the recod exists. So lets begin:

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

As before, Columns Parameter can be a single Database column, or multiple columns separated with comma . same thing with Record that represent the data inside each column

Thank for your help
Fadelovesky

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