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