vb.net vb2010 中添加、编辑、删除、搜索的代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14516653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:37:04  来源:igfitidea点击:

codes for ADD,EDIT,DELETE,SEARCH in vb2010

vb.netvisual-studio-2010

提问by Danjor

I'm currently working on my thesis in school and they required me to use VB2010 and MS ACCESS 2010.

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

what could be the easier way to connect and manipulate the DB? is it by using MS ACCESS 2003? or MS ACCESS 2007?

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

I need some help because I'm new to Visual Basic 2010

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

回答by Niranjan Singh

Have you googled about it - insert update delete access vb.net, there are lots of reference about this.

你有没有搜索过它 -插入更新删除访问 vb.net,有很多关于这个的参考。

Insert Update Delete Navigation & Searching In Access Database Using VB.NET

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

  • Create Visual Basic 2010 Project: VB-Access
  • Assume that, we have a database file named data.mdb
  • Place the data.mdb file into ..\bin\Debug\ folder (Where the project executable file (.exe) is placed)
  • 创建 Visual Basic 2010 项目:VB-Access
  • 假设,我们有一个名为 data.mdb 的数据库文件
  • 将data.mdb文件放入..\bin\Debug\文件夹(项目可执行文件(.exe)所在的位置)

what could be the easier way to connect and manipulate the DB?
Use OleDBConnection class to make connection with DB

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

is it by using MS ACCESS 2003 or MS ACCESS 2007?
you can use any you want to use or your client will use on their machine.

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

it seems that you want to find some example of opereations fo the database. Here is an example of Access 2010 for your reference:

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

Example code snippet:

示例代码片段:

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

Refer these links:
Insert, Update, Delete & Search Values in MS Access 2003 with VB.NET 2005
INSERT, DELETE, UPDATE AND SELECT Data in MS-Access with VB 2008
How Add new record ,Update record,Delete Records using Vb.net Forms when Access as a back

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

回答by Zeddy

A good resource start off point would be MSDNas your looking into a microsoft product

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