写入Access 2010数据库

时间:2013-07-03 17:32:16

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

我正在尝试使用vb.net写入访问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)

当我创建新数据库时,我必须创建新的 DataSet TableAdpater ,并且我还建立了与新数据库的连接。我必须忽略一些事情。

1 个答案:

答案 0 :(得分: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()