0% found this document useful (0 votes)
0 views

ADO.net object model -unit 4

The document provides an overview of database access in VB.Net using ADO.Net, detailing how applications communicate with databases to retrieve and update data. It explains the ADO.Net object model, including key components such as Connection, Command, DataReader, and DataAdapter, as well as the structure and functionality of DataSets. Additionally, it includes examples of connecting to a database, accessing data, and creating tables programmatically using VB.Net.

Uploaded by

hello4mjeba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
0 views

ADO.net object model -unit 4

The document provides an overview of database access in VB.Net using ADO.Net, detailing how applications communicate with databases to retrieve and update data. It explains the ADO.Net object model, including key components such as Connection, Command, DataReader, and DataAdapter, as well as the structure and functionality of DataSets. Additionally, it includes examples of connecting to a database, accessing data, and creating tables programmatically using VB.Net.

Uploaded by

hello4mjeba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 15
Page Lf 5, VB.Net - Database Access Applications communicate with a database, firstly, to retrieve the data stored there and present it in a user-friendly way, and secondly, to update the database by inserting, modifying and deleting data. Microsoft Activex Data Objects.Net (ADO.Net) is a model, a part of the .Net framework that is used by the .Net applications for retrieving, accessing and updating data. ADO.Net Object Model ADO.Net object model is nothing but the structured process flow through various components. The object model can be pictorially described as — oawerovder ata Cconnacton | [ Gutattopter DataReiationtaecton| ‘command oan DaeTabletalaton ole Detscounns » t [ Datsfeacer [OA] Datstore The data residing in a data store or database is retrieved through the data provider. Various components of the data provider retrieve data for the application and update data. ‘An application accesses data either through a dataset or a data reader. = Datasets store data in a disconnected cache and the application retrieves data from it. ™ Data readers provide data to the application in a read-only and forward-only mode. Data Provider A data provider is used for connecting to a database, executing commands and retrieving data, storing it in a dataset, reading the retrieved data and updating the database. The data provider in ADO.Net consists of the following four objects ~ Sr.No. Objects & Description Connection This component is used to set up a connection with a data source. Command 2 Acommand is a SQL statement or a stored procedure used to retrieve, insert, delete or modify data in a data source. DataReader 3 Data reader is used to retrieve data from a data source in a read-only and forward-only mode. DataAdapter This is integral to the working of ADO.Net since data is transferred to and 4 from a database through a data adapter. It retrieves data from a database into a dataset and updates the database. When changes are made to the dataset, the changes in the database are actually done by the data adapter. There are following different types of data providers included in ADO.Net = The .Net Framework data provider for SQL Server - provides access to Microsoft SQL Server. = The .Net Framework data provider for OLE DB - provides access to data sources exposed by using OLE DB. = The .Net Framework data provider for ODBC - provides access to data sources exposed by ODBC. = The .Net Framework data provider for Oracle - provides access to Oracle data source. "= The EntityClient provider - enables accessing data through Entity Data Model (EDM) applications. Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career. DataSet DataSet is an in-memory representation of data. It is a disconnected, cached set of records that are retrieved from a database. When a connection is established with the database, the data adapter creates a dataset and stores data in it. After the data is retrieved and stored in a dataset, the connection with the database is closed. This is, Page 25, Page 305, called the ‘disconnected architecture’, The dataset works as a virtual database containing tables, rows, and columns. The following diagram shows the dataset object model — S21 DataRelationcal DaraTableColection Extended? roperties Dataaelation DataTable | DataRowCollection Dataview Primaryey DataColumncellection | | The DataSet class is present in the System.Data namespace. The following table describes all the components of DataSet — Sr.No. Components & Description 1 DataTableCollection It contains all the tables retrieved from the data source 2 DataRelationCollection It contains relationships and the links between tables in a data set. ExtendedProperties 3 It contains additional information, like the SQL statement for retrieving data, time of retrieval, etc. DataTable 4 It represents a table in the DataTableCollection of a dataset. It consists of the DataRow and DataColumn objects. The DataTable objects are case-sensitive. DataRelation It represents a relationship in the DataRelationshipCollection of the dataset. It is used to relate two DataTable objects to each other through the DataColumn objects. 10 aL DataRowCollection It contains all the rows in a DataTable. Dataview It represents a fixed customized view of a DataTable for sorting, filtering, searching, ed \g and navigation. PrimaryKey It represents the column that uniquely identifies a row in a DataTable. DataRow It represents a row in the DataTable. The DataRow object and its properties and methods are used to retrieve, evaluate, insert, delete, and update values in the DataTable. The NewRow method is used to create a new row and the ‘Add method adds a row to the table. DataColumnCollection It represents all the columns in a DataTable. DataColumn It consists of the number of columns that comprise a DataTable. Connecting to a Database The .Net Framework provides two types of Connection classes — = SqlConnection — designed for connecting to Microsoft SQL Server. ™ OleDbConnection — designed for connecting to a wide range of databases, like Microsoft Access and Oracle. Example 1 We have a table stored in Microsoft SQL Server, named Customers, in a database named testDB. Please consult 'SQL Server’ tutorial for creating databases and database tables in SQL Server. Let us connect to this database. Take the following steps — Select TOOLS —+ Connect to Database Page 4 5, ™ Select a server name and the database name in the Add Connection dialog box. M TOOLS | Test waDOW HEL ie Connectto Database ‘de Shippets Manat Chile, Cine cod Attach te Process Cte AtteP Choose Toolbox ters. Library Package Manager , Brtensions and Updtes.. create GUD Error Lookup External Tools. Inport and Export Settings. Customie. Options.» Page Sof 5, Page 6 5, Enterinformation to connect tothe zelected data source or click "Change’ to | choatea ctferert data source and/or provider. Data source: Microsoft SQL Server (Sqilient) Server nome: ‘KABIR-DESKTOP Leg onto theserver © Use Windows Authentication (Use SOL Server Authentication Deseweta Save my password Connectto a database @ Select or ertera databsce name: (© Attach 2 databarefile Legiealnarre: Test Connection ™ Click on the Test Connection button to check if the connection succeeded. Microsc Visual Stale Expres 202 For Windows Oe. = = Add a DataGridView on the form. DataiView Tats Fat columns. ( Eraviezaeg (Enabling © atieteating Eel Gotan Feeding Deskin Part Conte —— | ™ Click on the Choose Data Source combo box. = Click on the Add Project Data Source link. (5 None] "W Add Project Dota Sources Clicethe ‘Ade Project Data Source. link to connect deta. = This opens the Data Source Configuration Wizard. ™ Select Database as the data source type crocs one ooe, =] Page 75, Page Bo 5, Bp carmen eri po gtd? @ & Serie One = sau) ‘Teectaiee med you chore deerme eps of at cs you opcaincoeus Aie e beesectovou weed = Choose the connection already set up. Page of 5, [5 Comeawnsting nt you vile nthe npn epi see ei) Dit Sacra CESR-DESTOPIntl Clogs ws neat Seay =Tue he ei ER ae ett Steing cemetery pleabn cenigton cena mirenc nd ely Test coatonstang he pcan Cnn eer sre xan ce he Deyo met oneve tection sng the apt ciation Be? B Yee acannon ™ Choose the database object, Customers table in our example, and click the Finish button, Page 10 0615 sexaimabea peer When the application is run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window — Page I of 5, 24) tutorilepoint.corm a | ae = : Te eri |kaushik Ee fines 1 3 | Hare jz WW iG Kona lel Example 2 In this example, let us access data in a DataGridView control using code. Take the following steps — = Add a DataGridView control and a button in the form. = Change the text of the button control to ‘Fill’. "= Double click the button control to add the required code for the Click event of the button, as shown below — Imports System.Data.SqlClient Public Class Form. Private Sub Formi_Load(sender As object, e As EventArgs) _ Handles MyBase.Load "ToD0: This line of code loads data into the ‘TestDBDataSet.CUSTOMERS’ tab: 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 Button Click(sender As Object, e As EventArgs) Handles Buttont..c: 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) Page 120615 Dim ds As DataSet = New Dataset() adp. Fill (ds) DataGridView1.DataSource = ds.Tables(2) End sub End Class ™ When the above code is executed and run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window — Creating Table, Columns and Rows Page 13 0f 15 We have discussed that the DataSet components like DataTable, DataColumn and DataRow allow us to create tables, columns and rows, respectively. The following example demonstrates the concept — Example 3 So far, we have used tables and databases already existing in our computer. In this example, we will create a table, add columns, rows and data into it and display the table using a DataGridView object. Take the following steps — = Add a DataGridView control and a button in the form. ™ Change the text of the button control to ‘Fill’. ™ Add the following code in the code editor. Public Class Form. Private Sub Form_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 colunns 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") Page 14 0f 15 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 columnNane As String) Dim column As Datacolumn = _ table.Colunns.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 Button4_Click(sender As Object, e As EventArgs) Handles Button1.c: Dim ds As New DataSet ds = Createbataset() DataGridView1.DataSource = ds.Tables("Student") End Sub End Class ™ When the above code is executed and run using Start button available at the Microsoft Visual Studio too! bar, it will show the following window — Page 15 of 15

You might also like