0

私はこんにちは専門家と言わなければなりません: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

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

Input:
Table = TableName
Columns = コンマで区切られた更新される列の名前 (例 1: "ID" 、例 2: "ID,Title,...")
Record() = 新しい値を表す文字列配列 ( 複数の値カンマで区切ります)

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

だから...問題は、実行時にMiss OleDBがこのエラーをスローすることです
.Microsoft Jetデータベースエンジンは入力テーブルまたはクエリ「IF」を見つけることができません....

前もって感謝します:D

4

2 に答える 2

0
 command.CommandType = CommandType.StoredProcedure 

ストアド プロシージャを実行していると主張しています (CommandText は既存の SProc の名前になります)。あなたが実際に直接実行するSQLコマンドを与えているのです。CommandType は Text である必要があります。

于 2010-08-27T13:47:06.933 に答える
0

私は自分の問題の解決策を見つけました (ちょっとしたネット調査で :) )
haha​​haha とにかく嬉しいです。データベース...しかし... :)

次に、興味深いメソッドを見つけました: 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

以前と同様に、Columns パラメータは単一のデータベース列、またはコンマで区切られた複数の列にすることができます。各列内のデータを表すレコードと同じこと

助けてくれて
ありがとう

于 2010-08-29T17:19:11.550 に答える