0

我目前正在学校写论文,他们要求我使用 VB2010 和 MS ACCESS 2010。

连接和操作数据库的更简单方法是什么?是使用 MS ACCESS 2003 吗?还是 MS ACCESS 2007?

我需要一些帮助,因为我是 Visual Basic 2010 的新手

4

2 回答 2

3

你用谷歌搜索过 - insert update delete access vb.net,有很多关于此的参考。

使用 VB.NET 在 Access 数据库中插入更新删除导航和搜索

  • 创建 Visual Basic 2010 项目:VB-Access
  • 假设,我们有一个名为 data.mdb 的数据库文件
  • 将 data.mdb 文件放入 ..\bin\Debug\ 文件夹(项目可执行文件 (.exe) 所在的位置)

连接和操作数据库的更简单方法是什么?
使用 OleDBConnection 类与 DB 建立连接

是使用 MS ACCESS 2003 还是 MS ACCESS 2007?
您可以使用任何您想使用的东西,或者您的客户将在他们的机器上使用。

似乎您想找到一些数据库操作的示例。以下是 Access 2010 的示例供您参考:

示例代码片段:

Imports System
Imports System.Data
Imports System.Data.OleDb

Public Class DBUtil

 Private connectionString As String

 Public Sub New()

  Dim con As New OleDb.OleDbConnection
  Dim dbProvider As String = "Provider=Microsoft.ace.oledb.12.0;"
  Dim dbSource = "Data Source=d:\DB\Database11.accdb"

  connectionString = dbProvider & dbSource

 End Sub

 Public Function GetCategories() As DataSet

  Dim query As String = "SELECT * FROM Categories"
  Dim cmd As New OleDbCommand(query)
  Return FillDataSet(cmd, "Categories")

 End Function

 Public SubUpdateCategories(ByVal name As String)
  Dim query As String = "update Categories set name = 'new2' where name = ?"
  Dim cmd As New OleDbCommand(query)
cmd.Parameters.AddWithValue("Name", name)
  Return FillDataSet(cmd, "Categories")

 End Sub

 Public Function GetItems() As DataSet

  Dim query As String = "SELECT * FROM Items"
  Dim cmd As New OleDbCommand(query)
  Return FillDataSet(cmd, "Items")

 End Function

 Public Function GetItems(ByVal categoryID As Integer) As DataSet

  'Create the command.
  Dim query As String = "SELECT * FROM Items WHERE Category_ID=?"
  Dim cmd As New OleDbCommand(query)
  cmd.Parameters.AddWithValue("category_ID", categoryID)

  'Fill the dataset.
  Return FillDataSet(cmd, "Items")

 End Function

 Public Sub AddCategory(ByVal name As String)

  Dim con As New OleDbConnection(connectionString)

  'Create the command.
  Dim insertSQL As String = "INSERT INTO Categories "
  insertSQL &= "VALUES(?)"
  Dim cmd As New OleDbCommand(insertSQL, con)
  cmd.Parameters.AddWithValue("Name", name)

  Try
   con.Open()
   cmd.ExecuteNonQuery()
  Finally
   con.Close()
  End Try

 End Sub

 Public Sub AddItem(ByVal title As String, ByVal description As String, _
    ByVal price As Decimal, ByVal categoryID As Integer)

  Dim con As New OleDbConnection(connectionString)

  'Create the command.
  Dim insertSQL As String = "INSERT INTO Items "
  insertSQL &= "(Title, Description, Price, Category_ID)"
  insertSQL &= "VALUES (?, ?, ?, ?)"
  Dim cmd As New OleDb.OleDbCommand(insertSQL, con)
  cmd.Parameters.AddWithValue("Title", title)
  cmd.Parameters.AddWithValue("Description", description)
  cmd.Parameters.AddWithValue("Price", price)
  cmd.Parameters.AddWithValue("CategoryID", categoryID)

  Try
   con.Open()
   cmd.ExecuteNonQuery()
  Finally
   con.Close()
  End Try

 End Sub

 Private Function FillDataSet(ByVal cmd As OleDbCommand, ByVal tableName As String) As DataSet

  Dim con As New OleDb.OleDbConnection
  Dim dbProvider As String = "Provider=Microsoft.ace.oledb.12.0;"
  Dim dbSource = "Data Source=D:\DB\Database11.accdb"

  connectionString = dbProvider & dbSource
  con.ConnectionString = connectionString
  cmd.Connection = con
  Dim adapter As New OleDbDataAdapter(cmd)
  Dim ds As New DataSet()

  Try
   con.Open()
   adapter.Fill(ds, tableName)
  Finally
   con.Close()
  End Try
  Return ds

 End Function

End Class

请参阅这些链接:
在 MS Access 2003 中使用 VB.NET 2005 插入、更新、删除和搜索值 在
MS-Access 中使用 VB 2008 插入、删除、更新和选择数据
如何使用 Vb.net 添加新记录、更新记录、删除记录Access 作为后台时的表单

于 2013-01-25T06:42:28.083 回答
0

MSDN是一个很好的资源起点,因为您正在研究 Microsoft 产品

于 2013-01-25T06:44:39.460 回答