このSQLクエリを修正:エラー「Microsoft Jetデータベースエンジンは、入力テーブルまたはクエリ「if」を見つけることができません」」

StackOverflow https://stackoverflow.com/questions/3584729

質問

私はこんにちは専門家と言うべきです:d。このきれいなコードで私を助けてください:)

データベース:

「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

最初の手順は、データベースフィールドにデータを追加するラッパーです(データが存在する場合)が、新しいデータが既に存在する行を行います。

入力:
表= TableName
列=更新されるコロンの名前コンマによって区切られます(ex1: "id"、ex2: "id、title、...")
record()=新しい値を表す文字列アレイ(複数の値はコンマで分離されます)

わかりました、データベースに値を追加する前に、この値で行が存在するかどうかを確認する必要があります:)
これを行うために、ストアドプロシージャを作成することは、データベースをすばやく処理する最良の方法です。

だから...今の問題は、ランタイムで、ミス・オレッドがこのエラーを投げるということです。
Microsoft Jetデータベースエンジンは、入力テーブルまたはクエリ「if」を見つけることができません。

事前に感謝:d

役に立ちましたか?

解決 2

私は自分の問題の解決策を見つけました(ちょっとした研究:))ハハハハim幸せ
とにかく、最初の質問は次のとおりです。「データベースが存在する場合にレコードを更新する方法」ので、データベースにストアドプロシージャを作成して保存しようとしました...しかし... :)

それから私は興味深い方法を見つけました: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

他のヒント

 command.CommandType = CommandType.StoredProcedure 

保存されたProcを実行していると主張しています(CommandTextは既存のSPROCの名前になります)。あなたが実際にそれを直接実行するためのSQLコマンドを与えているもの。 commandTypeはテキストである必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top