0

我正在尝试使用 vb.net 写入 access 2010 数据库。我让它工作但我不得不删除旧数据库并创建一个新数据库,而不是它不起作用。这是我的代码:

    Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Forte\Fortedb.accdb"
    Dim cnn As New OleDbConnection(ConnString)
    cnn.Open()

    'Naming the new Row in Query1.
    Dim newQuery1Row As FortedbDataSet.Query1Row
    newQuery1Row = Me.FortedbDataSet1.Query1.NewQuery1Row()

    'Making the first row the date.
    newQuery1Row.ProdDate = GlobalVariables.mdbProdDate
    newQuery1Row.BaleLine = GlobalVariables.mdbBaleLine
    newQuery1Row.BaleNumber = GlobalVariables.mdbBaleNumber
    newQuery1Row.GrossWeight = GlobalVariables.mdbGrossWeight
    newQuery1Row.AirDry = GlobalVariables.mdbAirDry
    newQuery1Row.InvoiceWeight = GlobalVariables.mdbInvoiceWeight

    'Adding the row to the table.
    Me.FortedbDataSet1.Query1.Rows.Add(newQuery1Row)

    cnn.Close()

    'Saving the row in Access.
    Me.Query1TableAdapter.Update(Me.FortedbDataSet1.Query1)

当我创建我的新数据库时,我必须创建新的DataSetTableAdpater,并且我还与新数据库建立了连接。我一定是忽略了什么。

4

1 回答 1

0

我已经想通了。我决定用一条 SQL 语句来解决这个问题。

Dim connection As OleDb.OleDbConnection mystr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Forte\Fortedb.accdb" connection = New OleDb.OleDbConnection(mystr) connection.Open() 'Inserting the data into the database. mydb = "INSERT INTO b_forte (ProdDate, BaleLine, BaleNumber, GrossWeight, AirDry, InvoiceWeight) VALUES ('" & mdbProdDate & "', '" & mdbBaleLine & "', '" & mdbBaleNumber & "', '" & mdbGrossWeight & "', '" & mdbAirDry & "', '" & mdbInvoiceWeight & "')" Dim run = New OleDb.OleDbCommand Try run = New OleDbCommand(mydb, connection) run.ExecuteNonQuery() connection.Close()

于 2013-07-10T15:48:47.800 回答