You are on page 1of 8

Unit III

What is ADO.NET?
ADO stands for Microsoft ActiveX Data Objects. ADO.NET is one of Microsoft’s Data Access technology using which we can communicate
with different data sources. It is a part of the .Net Framework which is used to establish a connection between the .NET Application and data
sources. The Data sources can be SQL Server, Oracle, MySQL, and XML, etc. ADO.NET consists of a set of classes that can be used to
connect, retrieve, insert, update and delete data (i.e. performing CRUD operation) from data sources. ADO.NET mainly
uses System.Data.dll and System.Xml.dll.

accessing data using server explorer in VB.NET

Now Use this Method to Create a New Connection to Data Source using the Server Explorer.

1. Click “Connect to Database” in the Server Explorer window (if its not visible, you can open it from the View menu, then click
Server Explorer.)
2. Follow these steps in the Data Link Properties dialog box:
a. In the Select or enter a server name text box, type localhost.
b. Under Enter information to log on to the server, ( if you need security click Use Windows NT
Integrated security).
3. Click Select the database on the server, and then select Database from the list of databases(eg Northwind ).
a. Click Test Connection to verify that the information is correct, and then click OK.
4. In the Server Explorer window, expand the Data Connections, the YourServerName.Northwind.dbo, and the Tables nodes.
5. Right-click Categories, and then click Retrieve Data from Table(this will fetch the Columns(Attributes) from the Database.
6. Edit one of the product descriptions, and then move to another record. Notice: These change affects the underlying data
source.
7. Right-click Categories, and then click Design Table. Change the length of the CategoryName column to 20(i.e size). Click
Save, and then verify the changes in the updated table.
8. Right-click Views, and then click New View. Add the Customers table to the view, and then click OK to close the Add Table
dialog box.
9. In the Customers table, select the following columns:CompanyNameContactNameContactTitle
10. Right-click in the SQL pane, and then click Verify SQL Syntax.
11. Right-click in the diagram pane, and then click Run.
12. Click Save, and then type CustomerInfo for the view.
Now to Display Data in a Windows Form

1. View yourForm in which you want to display Data in the Design window.
2. Drag the Categories node from the Server Explorer window to Form1 (yourForm), which adds the SqlConnection and the
SqlDataAdapter objects to the form. View the properties of these objects to verify that they are configured to connect to the
Categories table in the Northwind database.
3. Right-click SqlDataAdapter1, and then click Generate Dataset. Click OK in the Generate Dataset dialog box.
4. Add a DataGrid control to the form, and then set the DataSource property of the DataGrid to DataSet11.Categories.
5. Create an event handler for the Form_Load event, and then add the following code to this event:
1. SqlDataAdapter1.Fill(DataSet11) 

1. On the File menu, click Save All.


2. Run the application, and then verify that the data is displayed correctly

Connection

Connection String is a normal String representation which contains Database connection information to


establish the connection between Datbase and the Application. The Connection String includes parameters
such as the name of the driver, Server name and Database name , as well as security information such as user
name and password. Data providers use a connection string containing a collection of parameters to establish
the connection with the database.
The .NET Framework provides mainly three data providers: Microsoft SQL Server, OLEDB and ODBC. Here
you can see how to make connection string to these ADO.NET Data Providers.

SQL Server Connection

You can connect your VB.Net application to data in a SQL Server database using the Microsoft .NET
Framework Data Provider for SQL Server. The first step in a VB.Net application is to create an instance of the
Server object and to establish its connection to an instance of SQL Server.

The SqlConnection Object is Handling the part of physical communication between the application and
the SQL Server Database. An instance of the SqlConnection class in .NET Framework is supported the Data
Provider for SQL Server Database. The SqlConnection instance takes Connection String as argument and pass
the value to the Constructor statement. When the connection is established , SQL Commands may be executed,
with the help of the Connection Object, to retrieve or manipulate data in the database. Once the Database
activities over , Connection should be closed and release the database resources .

Sql Server connection string

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;

User ID=UserName;Password=Password"

A Sample VB.Net Program that connect SQL Server using connection string.

Imports System.Data.SqlClient

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button1.Click
Dim connetionString As String

Dim cnn As SqlConnection

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User


ID=UserName;Password=Password"

cnn = New SqlConnection(connetionString)

Try

cnn.Open()

MsgBox("Connection Open ! ")

cnn.Close()

Catch ex As Exception

MsgBox("Can not open connection ! ")

End Try

End Sub

End Class

Ole

An OleDbConnection object supports a connection to an OLE DB data provider. In


practice, you usually use OLE DB connections with all data providers except
Microsoft's SQL Server. Note that, depending on the OLE DB data provider, not
all properties of an OleDbConnection object may be supported.

A central property of connection objects is the ConnectionString property, which


holds a string full of attribute/value pairs that contain data needed to log on to a
data provider and choose a specific database. These attribute/value pairs are
specific to the data provider you're using, and make up a list of items separated by
semicolons. You can either assign a connection string to the connection's
ConnectionString property, or you can pass the connection string to the connection
object's constructor, like this:

Imports System.Data

Module Test

Sub Main()
Dim sConnectionString, sSQL As String

'SQL Connection String


sConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\yourdatabase.mdb"

sSQL = "SELECT Title FROM yourTable"

Dim conn As New


System.Data.OleDb.OleDbConnection(sConnectionString)
Dim cmd As New System.Data.OleDb.OleDbCommand(sSQL,
conn)
Dim dr As System.Data.OleDb.OleDbDataReader
conn.Open()

dr = cmd.ExecuteReader()

Do While dr.Read()
System.Console.WriteLine(dr.Item("Title"))
Loop

dr.Close()
conn.Close()
End Sub
End Module

What is command

The command object is used to execute select statements, insert, update, or delete
statements, stored procedures, or any other statement which is defined by database. ...
NET Framework Data Provider for SQL Server includes a SqlCommand object and oledb
includes an OleDbCommand object

SqlCommand Class
 

The main role of SqlCommand is to execute SQL statements.

 SqlCommand Properties

 CommandText: The commandText property is used to set the SQL


statement.

 Connection: This property is used to get connection to the database


source which is specified in SqlConnection. 

 SqlCommand Method

 ExecuteNonQuery() : The ExecuteNonQuery() method does not return


any record. Which means we use ExecuteNonQuery() in all operations with
databases except retrieving records from a database. It only returns
the number of affected rows.

 Now we use this method in our application. There is a database "student"


and a database "student_detail" which has no record. I am giving a simple
example to insert a record into database

What is DataAdapter ?

The DataAdapter works as a bridge between a DataSet and a data source to


retrieve data. DataAdapter is a class that represents a set of SQL commands
and a database connection. It can be used to fill the DataSet and update the
data source.

SqlDataAdapter sde = new SqlDataAdapter("Select * from student", con);  

What is DataSet ?

DataSet. This type stores many DataTables in VB.NET programs. A


DataSet is conceptually a set of DataTables and other information about
those tables.

DataSet With OLEDB


Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim connectionString As String =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="Your .mdb path";"
Dim sql As String = "SELECT * FROM Authors"
Dim connection As New OleDbConnection(connectionString)
Dim dataadapter As New OleDbDataAdapter(sql, connection)
Dim ds As New DataSet()
connection.Open()
dataadapter.Fill(ds, "Authors_table")
connection.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Authors_table"
End Sub
End Class
DataSet With OLEDB
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

The Data-Bound Controls: 

You can bind certain controls to the data control, the remote data control, and the ADO data
control, and those controls are called bound controls. To bind a control to a database control, you
use properties like DataSource to specify the database control, and then use properties
like DataField or BoundColumn to specify what field to display in the bound control, as we’ll
see. Here are the controls that can function as bound controls:
• Picture boxes
• Labels
• Text boxes
• Checkboxes
• Image controls
• OLE controls
• List boxes
• Masked edit controls
• Rich text boxes
• Combo boxes
In addition, there are special controls that are designed to be used as bound controls:
• DBList
• DBCombo
• FlexGrid
• MSFlexGrid
Finally, a number of bound controls are specially built to be used with the ADO control only:
• DataList
• DataCombo
• DataGrid

What is a data grid?


A data grid is a set of computers that directly interact with each other to coordinate the processing of large jobs. The participating
computers are typically spread across multiple geographically remote sites. Each site may have one or more of the computers in the
grid, and each site shares data and resources with other sites. The main goal of a data grid is to leverage the collective power of all
computers to accomplish a given task, in a practice known as grid computing. Software running on all the computers in a grid
handles the coordination of tasks, and user accesses to data, across the grid.

You might also like