VB.Net - 資料庫訪問



應用程式與資料庫通訊,首先,檢索儲存在資料庫中的資料並以使用者友好的方式呈現,其次,透過插入、修改和刪除資料來更新資料庫。

Microsoft ActiveX 資料物件 .Net (ADO.Net) 是一種模型,它是 .Net 框架的一部分,.Net 應用程式使用它來檢索、訪問和更新資料。

ADO.Net 物件模型

ADO.Net 物件模型只不過是透過各種元件的結構化流程。物件模型可以用圖示描述為:

ADO.Net objects

駐留在資料儲存或資料庫中的資料透過資料提供程式檢索。資料提供程式的各種元件為應用程式檢索資料並更新資料。

應用程式可以透過資料集或資料讀取器訪問資料。

  • 資料集將資料儲存在斷開連線的快取中,應用程式從中檢索資料。

  • 資料讀取器以只讀和向前只讀模式為應用程式提供資料。

資料提供程式

資料提供程式用於連線到資料庫、執行命令和檢索資料、將其儲存在資料集中、讀取檢索到的資料以及更新資料庫。

ADO.Net 中的資料提供程式包含以下四個物件:

序號 物件和描述
1

連線

此元件用於建立與資料來源的連線。

2

命令

命令是 SQL 語句或儲存過程,用於檢索、插入、刪除或修改資料來源中的資料。

3

資料讀取器

資料讀取器用於以只讀和向前只讀模式從資料來源檢索資料。

4

資料介面卡

這是 ADO.Net 工作的組成部分,因為資料是透過資料介面卡傳輸到資料庫和從資料庫傳輸的。它從資料庫檢索資料到資料集並更新資料庫。當對資料集進行更改時,資料庫中的更改實際上是由資料介面卡完成的。

ADO.Net 中包含以下不同型別的資料提供程式

  • SQL Server 的 .Net Framework 資料提供程式 - 提供對 Microsoft SQL Server 的訪問。

  • OLE DB 的 .Net Framework 資料提供程式 - 提供對使用 OLE DB 公開的數

  • ODBC 的 .Net Framework 資料提供程式 - 提供對 ODBC 公開的數

  • Oracle 的 .Net Framework 資料提供程式 - 提供對 Oracle 資料來源的訪問。

  • EntityClient 提供程式 - 支援透過實體資料模型 (EDM) 應用程式訪問資料。

資料集

資料集是資料的記憶體表示。它是從資料庫檢索到的斷開連線的快取記錄集。當與資料庫建立連線時,資料介面卡建立資料集並將資料儲存在其中。檢索並存儲在資料集中後,與資料庫的連線將關閉。這稱為“斷開連線體系結構”。資料集充當包含表、行和列的虛擬資料庫。

下圖顯示了資料集物件模型:

VB.Net Data Classes

DataSet 類位於System.Data名稱空間中。下表描述了 DataSet 的所有元件:

序號 元件和描述
1

DataTableCollection

它包含從資料來源檢索的所有表。

2

DataRelationCollection

它包含資料集表之間的關係和連結。

3

ExtendedProperties

它包含其他資訊,例如檢索資料的 SQL 語句、檢索時間等。

4

DataTable

它表示資料集中 DataTableCollection 中的一個表。它由 DataRow 和 DataColumn 物件組成。DataTable 物件區分大小寫。

5

DataRelation

它表示資料集中 DataRelationshipCollection 中的關係。它用於透過 DataColumn 物件將兩個 DataTable 物件彼此關聯。

6

DataRowCollection

它包含 DataTable 中的所有行。

7

DataView

它表示 DataTable 的固定自定義檢視,用於排序、過濾、搜尋、編輯和導航。

8

PrimaryKey

它表示唯一標識 DataTable 中行的列。

9

DataRow

它表示 DataTable 中的一行。DataRow 物件及其屬性和方法用於檢索、評估、插入、刪除和更新 DataTable 中的值。NewRow 方法用於建立新行,Add 方法將行新增到表中。

10

DataColumnCollection

它表示 DataTable 中的所有列。

11

DataColumn

它由構成 DataTable 的列陣列成。

連線到資料庫

.Net Framework 提供兩種型別的連線類:

  • SqlConnection - 用於連線到 Microsoft SQL Server。

  • OleDbConnection - 用於連線到各種資料庫,如 Microsoft Access 和 Oracle。

示例 1

我們在 Microsoft SQL Server 中儲存了一個名為 Customers 的表,位於名為 testDB 的資料庫中。請參閱“SQL Server”教程以瞭解如何在 SQL Server 中建立資料庫和資料庫表。

讓我們連線到此資料庫。請執行以下步驟:

  • 選擇工具 → 連線到資料庫

VB.Net Database connection Example
  • 在“新增連線”對話方塊中選擇伺服器名稱和資料庫名稱。

    M
VB.Net Database Connection
  • 單擊“測試連線”按鈕以檢查連線是否成功。

Connection Success
  • 在窗體上新增一個 DataGridView。

VB.Net DataGridView
  • 單擊“選擇資料來源”組合框。

  • 單擊“新增專案資料來源”連結。

Add Project Data Source Link
  • 這將開啟“資料來源配置嚮導”。

  • 選擇資料庫作為資料來源型別

Data Source
  • 選擇資料集作為資料庫模型。

Database Model
  • 選擇已設定的連線。

VB.Net Database Connection
  • 儲存連線字串。

Saving the connection string
  • 選擇資料庫物件(在我們的示例中為 Customers 表),然後單擊“完成”按鈕。

VB.Net database connection
  • 選擇“預覽資料”連結以在“結果”網格中檢視資料:

Data Preview

當使用 Microsoft Visual Studio 工具欄中提供的啟動按鈕執行應用程式時,它將顯示以下視窗:

VB.net data in data grid view

示例 2

在此示例中,讓我們使用程式碼訪問 DataGridView 控制元件中的資料。請執行以下步驟:

  • 在窗體中新增一個 DataGridView 控制元件和一個按鈕。

  • 將按鈕控制元件的文字更改為“填充”。

  • 雙擊按鈕控制元件以新增按鈕的 Click 事件所需的程式碼,如下所示:

Imports System.Data.SqlClient
Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) _
   Handles MyBase.Load
      'TODO: This line of code loads data into the 'TestDBDataSet.CUSTOMERS' table.   
      You can move, or remove it, as needed.
      
      Me.CUSTOMERSTableAdapter.Fill(Me.TestDBDataSet.CUSTOMERS)
      ' Set the caption bar text of the form.   
      Me.Text = "tutorialspoint.com"
   End Sub
   
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Dim connection As SqlConnection = New sqlconnection()
      connection.ConnectionString = "Data Source=KABIR-DESKTOP; _
         Initial Catalog=testDB;Integrated Security=True"
      connection.Open()
      Dim adp As SqlDataAdapter = New SqlDataAdapter _
      ("select * from Customers", connection)
      Dim ds As DataSet = New DataSet()
      adp.Fill(ds)
      DataGridView1.DataSource = ds.Tables(0)
   End Sub
End Class
  • 當使用 Microsoft Visual Studio 工具欄中提供的啟動按鈕執行並執行上述程式碼時,它將顯示以下視窗:

Database Connectivity
  • 單擊“填充”按鈕將在資料網格檢視控制元件上顯示錶:

Database connectivity

建立表、列和行

我們已經討論過,DataSet 元件(如 DataTable、DataColumn 和 DataRow)允許我們分別建立表、列和行。

以下示例演示了該概念:

示例 3

到目前為止,我們使用的是計算機中已存在的表和資料庫。在此示例中,我們將建立一個表,向其中新增列、行和資料,並使用 DataGridView 物件顯示該表。

請執行以下步驟:

  • 在窗體中新增一個 DataGridView 控制元件和一個按鈕。

  • 將按鈕控制元件的文字更改為“填充”。

  • 在程式碼編輯器中新增以下程式碼。

Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Set the caption bar text of the form.   
      Me.Text = "tutorialspont.com"
   End Sub
   
   Private Function CreateDataSet() As DataSet
      'creating a DataSet object for tables
      Dim dataset As DataSet = New DataSet()
      ' creating the student table
      Dim Students As DataTable = CreateStudentTable()
      dataset.Tables.Add(Students)
      Return dataset
   End Function
   
   Private Function CreateStudentTable() As DataTable
      Dim Students As DataTable
      Students = New DataTable("Student")
      ' adding columns
      AddNewColumn(Students, "System.Int32", "StudentID")
      AddNewColumn(Students, "System.String", "StudentName")
      AddNewColumn(Students, "System.String", "StudentCity")
      ' adding rows
      AddNewRow(Students, 1, "Zara Ali", "Kolkata")
      AddNewRow(Students, 2, "Shreya Sharma", "Delhi")
      AddNewRow(Students, 3, "Rini Mukherjee", "Hyderabad")
      AddNewRow(Students, 4, "Sunil Dubey", "Bikaner")
      AddNewRow(Students, 5, "Rajat Mishra", "Patna")
      Return Students
   End Function
   
   Private Sub AddNewColumn(ByRef table As DataTable, _ 
   ByVal columnType As String, ByVal columnName As String)
      Dim column As DataColumn = _ 
         table.Columns.Add(columnName, Type.GetType(columnType))
   End Sub

   'adding data into the table
   Private Sub AddNewRow(ByRef table As DataTable, ByRef id As Integer,_
   ByRef name As String, ByRef city As String)
      Dim newrow As DataRow = table.NewRow()
      newrow("StudentID") = id
      newrow("StudentName") = name
      newrow("StudentCity") = city
      table.Rows.Add(newrow)
   End Sub
   
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Dim ds As New DataSet
      ds = CreateDataSet()
      DataGridView1.DataSource = ds.Tables("Student")
   End Sub
End Class
  • 當使用 Microsoft Visual Studio 工具欄中提供的啟動按鈕執行並執行上述程式碼時,它將顯示以下視窗:

Example
  • 單擊“填充”按鈕將在資料網格檢視控制元件上顯示錶:

Example
廣告