應(yīng)用程序與數(shù)據(jù)庫通信,首先,檢索存儲在那里的數(shù)據(jù),并以用戶友好的方式呈現(xiàn)它,其次,通過插入,修改和刪除數(shù)據(jù)來更新數(shù)據(jù)庫。
Microsoft ActiveX Data Objects.Net(ADO.Net)是一個模型,.Net框架的一部分,由.Net應(yīng)用程序用于檢索,訪問和更新數(shù)據(jù)。
ADO.Net對象模型只不過是通過各種組件的結(jié)構(gòu)化流程。 對象模型可以被圖形描述為:
通過數(shù)據(jù)提供者檢索駐留在數(shù)據(jù)存儲或數(shù)據(jù)庫中的數(shù)據(jù)。 數(shù)據(jù)提供者的各種組件檢索應(yīng)用程序的數(shù)據(jù)并更新數(shù)據(jù)。
應(yīng)用程序通過數(shù)據(jù)集或數(shù)據(jù)讀取器訪問數(shù)據(jù)。
Datasets 數(shù)據(jù)集:數(shù)據(jù)集將數(shù)據(jù)存儲在斷開連接的緩存中,應(yīng)用程序從中檢索數(shù)據(jù)。
Data readers 數(shù)據(jù)讀?。?/strong>數(shù)據(jù)讀取器以只讀和僅轉(zhuǎn)發(fā)模式向應(yīng)用程序提供數(shù)據(jù)。
SN | 對象和說明 |
---|---|
1 | Connection 該組件被用來建立與數(shù)據(jù)源的連接。 |
2 | Command 命令是用于檢索,插入,刪除或修改數(shù)據(jù)源中的數(shù)據(jù)的SQL語句或存儲過程。 |
3 | DataReader 數(shù)據(jù)讀取器用于以只讀和僅轉(zhuǎn)發(fā)模式從數(shù)據(jù)源檢索數(shù)據(jù)。 |
4 | DataAdapter 這是ADO.Net的工作的組成部分,因為數(shù)據(jù)通過數(shù)據(jù)適配器傳輸?shù)綌?shù)據(jù)庫和從數(shù)據(jù)庫傳輸。 它將數(shù)據(jù)從數(shù)據(jù)庫檢索到數(shù)據(jù)集并更新數(shù)據(jù)庫。 當(dāng)對數(shù)據(jù)集進(jìn)行更改時,數(shù)據(jù)庫中的更改實際上由數(shù)據(jù)適配器完成。 |
ADO.Net中包含以下不同類型的數(shù)據(jù)提供程序
SQL Server的.Net Framework數(shù)據(jù)提供者 - 提供對Microsoft SQL Server的訪問。
OLE DB的.Net Framework數(shù)據(jù)提供者 - 提供對使用OLE DB公開的數(shù)據(jù)源的訪問。
ODBC的.Net Framework數(shù)據(jù)提供程序 - 提供對ODBC公開的數(shù)據(jù)源的訪問。
Oracle的.Net Framework數(shù)據(jù)提供程序 - 提供對Oracle數(shù)據(jù)源的訪問。
EntityClient提供程序 - 允許通過實體數(shù)據(jù)模型(EDM)應(yīng)用程序訪問數(shù)據(jù)。
DataSet是數(shù)據(jù)的內(nèi)存表示。 它是從數(shù)據(jù)庫檢索的斷開連接的高速緩存的記錄集。 當(dāng)與數(shù)據(jù)庫建立連接時,數(shù)據(jù)適配器創(chuàng)建數(shù)據(jù)集并在其中存儲數(shù)據(jù)。 在檢索數(shù)據(jù)并將其存儲在數(shù)據(jù)集中之后,將關(guān)閉與數(shù)據(jù)庫的連接。 這被稱為“斷開連接的架構(gòu)”。 數(shù)據(jù)集用作包含表,行和列的虛擬數(shù)據(jù)庫。
下圖顯示了數(shù)據(jù)集對象模型:
DataSet類存在于System.Data命名空間中。 下表描述了DataSet的所有組件:
SN | 組件及說明 |
---|---|
1 | DataTableCollection 它包含了從數(shù)據(jù)源中檢索的所有表。 |
2 | DataRelationCollection 它包含數(shù)據(jù)集中的表之間的關(guān)系和鏈接。 |
3 | ExtendedProperties 它包含的其他信息,例如用于檢索數(shù)據(jù)的SQL語句,檢索的時間等 |
4 | DataTable 它表示數(shù)據(jù)集的DataTableCollection中的表。它由DataRow和DataColumn對象組成。 DataTable對象區(qū)分大小寫。 |
5 | DataRelation 它表示數(shù)據(jù)集的DataRelationshipCollection中的關(guān)系。它用于通過DataColumn對象將兩個DataTable對象相互關(guān)聯(lián)。 |
6 | DataRowCollection 它包含DataTable中的所有行。 |
7 | DataView 它表示用于排序,過濾,搜索,編輯和導(dǎo)航的DataTable的固定自定義視圖。 |
8 | PrimaryKey 它表示唯一標(biāo)識DataTable中某一行的列。 |
9 | DataRow 它表示DataTable中的一行。 DataRow對象及其屬性和方法用于檢索,評估,插入,刪除和更新DataTable中的值。 NewRow方法用于創(chuàng)建一個新行,Add方法向表中添加一行。 |
10 | DataColumnCollection 它表示DataTable中的所有列。 |
11 | DataColumn 它由組成DataTable的列數(shù)組成。 |
.Net框架提供兩種類型的Connection類:
SqlConnection -設(shè)計用于連接到Microsoft SQL Server。
OleDbConnection -設(shè)計用于連接到各種數(shù)據(jù)庫,如Microsoft Access和Oracle。
我們有一個表存儲在名為testDB的數(shù)據(jù)庫中的名為Customers的Microsoft SQL Server中。 有關(guān)在SQL Server中創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù)庫表的信息,請參考“SQL Server”教程。
讓我們連接到此數(shù)據(jù)庫。 執(zhí)行以下步驟:
選擇工具 - >連接到數(shù)據(jù)庫
在“添加連接”對話框中選擇服務(wù)器名稱和數(shù)據(jù)庫名稱。
單擊測試連接按鈕以檢查連接是否成功。
在表單上添加一個DataGridView。
單擊選擇數(shù)據(jù)源組合框。
單擊添加項目數(shù)據(jù)源鏈接。
這將打開“數(shù)據(jù)源配置向?qū)А薄?/span>
選擇“數(shù)據(jù)庫”作為數(shù)據(jù)源類型
選擇的DataSet作為數(shù)據(jù)庫模型。
選擇已設(shè)置的連接。
保存連接字符串。
在我們的示例中選擇數(shù)據(jù)庫對象Customers表,然后單擊完成按鈕。
選擇“預(yù)覽數(shù)據(jù)”鏈接以查看“結(jié)果”網(wǎng)格中的數(shù)據(jù):
當(dāng)使用Microsoft Visual Studio工具欄上的“開始”按鈕運行應(yīng)用程序時,將顯示以下窗口:
在這個例子中,讓我們使用代碼訪問DataGridView控件中的數(shù)據(jù)。 執(zhí)行以下步驟:
在窗體中添加一個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
當(dāng)使用Microsoft Visual Studio工具欄上的“開始”按鈕執(zhí)行并運行上述代碼時,將顯示以下窗口:
單擊“填充”按鈕可顯示數(shù)據(jù)網(wǎng)格視圖控件上的表:
我們已經(jīng)討論過,像DataTable,DataColumn和DataRow這樣的DataSet組件允許我們分別創(chuàng)建表,列和行。
下面的例子演示了這個概念:
到目前為止,我們已經(jīng)使用我們的計算機(jī)中已經(jīng)存在的表和數(shù)據(jù)庫。 在本示例中,我們將創(chuàng)建一個表,向其中添加列,行和數(shù)據(jù),并使用DataGridView對象顯示表。
執(zhí)行以下步驟:
在窗體中添加一個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
當(dāng)使用Microsoft Visual Studio工具欄上的“開始”按鈕執(zhí)行并運行上述代碼時,將顯示以下窗口
單擊“填充”按鈕可顯示數(shù)據(jù)網(wǎng)格視圖控件上的表:
更多建議: