添加记录以访问数据库 [英] add records to access database
本文介绍了添加记录以访问数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚接触vb.net编程。我想将记录添加到访问数据库表中。使用以下代码时,我收到错误消息NullReferenceException未处理。任何人
帮助纠正此问题。
Iam new to vb.net programming.I want to add records to an access database table. I get the error message "NullReferenceException was unhandled" when using the following code.Can anyone
help to correct this.
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim inc As Integer
dbProvider = "provider=microsoft.ACE.OLEDB.12.0;"
dbSource = "DATA SOURCE=C:\cwhData\cwhData.accdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "select * from customer"
da = New OleDb.OleDbDataAdapter(sql, con)
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("customer").NewRow()
dsNewRow.Item("CUSTOMERID") = txtCustomerID.Text
dsNewRow.Item("customername") = txtCustomerName.Text
ds.Tables("customer").Rows.Add(dsNewRow)
da.Update(ds, "customer")
MsgBox("New record added to the database")
End If
MsgBox("database is now open")
con.Close()
MsgBox("database is now closed")
End Sub
推荐答案
从这里开始:访问Microsoft .NET应用程序中的Office数据 [ ^ ]
更多例子,你会在这里找到: CP知识库 [ ^ ]
Start here: Accessing Microsoft Office Data from .NET Applications[^]
More examples, you'll find here: CP Knowledge Base[^]
这篇关于添加记录以访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文