VB.NET将数据插入MS Access DB

时间:2019-03-14 17:40:23

标签: .net vb.net ms-access oledb

这是我第一次尝试使用.NET和数据库。

我正在尝试将记录添加到表中,但未添加任何内容。我将其简化为下面的基本代码。

不会产生任何错误,但不会向表中添加任何内容。

Imports System.Data
'Imports System.Data.OleDb
Class Form1
    Dim dbProvider As String
    Dim dbSource As String

    Dim dbPathAndFilename As String
    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" ' MDB
        'dbProvider = "PROVIDER=Microsoft.Ace.OLEDB.12.0;" 'ACCDB
        dbSource = "Data Source="

        LoadData()
    End Sub

    Sub LoadData()
        'Connect to db
        'You could store the db path in the Settings of the App.
        'dbPathAndFilename = My.Settings.dbPath
        dbPathAndFilename = "C:\temp\VB\DBTest\Test.mdb"
        con.ConnectionString = dbProvider & dbSource & dbPathAndFilename

        con.Open()
        sql = "INSERT INTO Table1(Field1) VALUES('Field1');"
        da = New OleDb.OleDbDataAdapter(sql, con)
        con.Close()
    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

尝试一下

Class Form1
    Private dbProvider As String
    Private dbPathAndFilename As String

    Sub LoadData()
        dbProvider = "Microsoft.Jet.OLEDB.4.0;"
        dbPathAndFilename = "C:\temp\VB\DBTest\Test.mdb"
        Using con As New OleDb.OleDbConnection($"PROVIDER={dbProvider};DATA SOURCE={dbPathAndFilename}")
            con.Open()
            Using cmd = con.CreateCommand()
                cmd.CommandText = "INSERT INTO Table1([Field1]) VALUES(@field1);"
                cmd.Parameters.AddWithValue("@field1", field1Value) ' replace field1Value with your value
                cmd.ExecuteNonQuery()
            End Using
        End Using
    End Sub

End Class

与您的版本相比,它具有一些优势。

  1. 它用Using块限制了一次性物品的范围
  2. 使用首选参数以避免注入
  3. 我觉得连接字符串有点笨拙,这使它显得有点
  4. 最重要的是,引入了Command,因此可以调用ExecuteNonQuery,这与您在查询中返回数据的链接示例相反。

答案 1 :(得分:0)

Imports System.Data
Imports System.Data.OleDb

Class Form1
    Dim dbProvider As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" ' MDB
    Dim dbSource As String = "Data Source="
    Dim dbPathAndFilename As String

    Public Property ConnectionString as String
       Get
          return $"{dbProvider}{dbSource}{dbPathAndFilename}"
       End Get
    End Property

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dbPathAndFilename = "C:\temp\VB\DBTest\Test.mdb"    
        LoadData()
    End Sub

    Sub LoadData()
        Dim sql As String = "INSERT INTO Table1(Field1) VALUES(?);"

        Using con As New OleDbConnection(ConnectionString), _
              cmd As New OleDbCommand(sql)

            cmd.Parameters.Add("Field1", OleDbType.VarWString, 50).Value = "Field1"

            con.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Sub
End Class