2023年6月27日 星期二

VBNET InsertRow DBF

 Private Sub InsertRow(ByVal Field1 As String, ByVal Field2 As String, ByVal Field3 As String)

        'see above 

        'here you would set what Typ of Data

        ' I just used Field1 to 3 as a String


        Dim con As OleDbConnection

        Dim dBaseFile As String


        dBaseFile = "TestData.dbf"

        Try

            con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\;Extended Properties=dBASE IV;")


            'Create a new instance of the command object

            Using cmd As OleDbCommand = New OleDbCommand("INSERT INTO " & dBaseFile & " ([Field1], [Field2], [Field3]) VALUES (@f1, @f2, @f3)", con)

                'Parameterize the query

                With cmd.Parameters

                    .AddWithValue("@f1", Field1)

                    .AddWithValue("@f2", Field2)

                    .AddWithValue("@f3", Field3)

                End With

                con.Open()

                cmd.ExecuteNonQuery()

                con.Close()

            End Using

        Catch ex As Exception

            Console.WriteLine(ex.Message)

        Finally

            If con IsNot Nothing Then

                If con.State = ConnectionState.Open Then

                    con.Close()

                End If

                con.Dispose()

            End If

        End Try

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'fill Textboxes and ADD Datarow

        InsertRow(Field1:=TextBox1.Text, Field2:=TextBox2.Text, Field3:=TextBox3.Text)

    End Sub


沒有留言:

張貼留言