How To Access, Insert, Update and Delete Data in A SQL Server 2005
How To Access, Insert, Update and Delete Data in A SQL Server 2005
Object
Connection
Object
Command
Object
Description
Creates a connection to your data source
between two.
Fig:1.1 - Relationship between Visual Basic.Net application, ADO.Net data access components and
SQL Server
Connection
Command
DataReader
DataAdapter
Let us try and understand how the ADO.Net Objects fits into the scheme of programming in Visual
Basic .Net.
Firstly, observe that the object model is sought to be explained in relationship to the database and
the Visual Basic.net application. Hence, the shading of the Visual Basic.net and database blocks.
We also note that the four components are shown grouped as Data Provider. The dataset is not
categorized as data provider as the dataset is a temporary storage mechanism or representation of
Connection object
The Connection object establishes a connection to the database. Two of the most common
Connection objects used are OleDbConnection and SqlConnection. Both the SqlConnection and
OleDbConnection namespaces inherit from the IdbConnection object. The important property of the
Connection object is ConnectionString and State property and the important method is Open()
method.
ConnectionString Property
It provides information, such as the data source and database name, that is used to establish
connection with a database.
Here is an example of the ConnectionString property.
Dim conFinAccounting As New SqlConnection
conFinAccounting.ConnectionString="Data Source=(local); Initial Catalog=FinAccounting; Integrated
Security=SSPI;"
Replace the data source value with the name of your SQL Server, or keep the local setting if you are
running SQL Server on the same machine. Set the value for Integrated Security as SSPI which
specifies the secure connection.
ExecuteNonQuery
We will use this method when we want to execute a non-row returning command such as a DELETE
Statement as shown.
comAccounts.ExecuteNonQuery
ExecuteReader
This method should be used when we want to execute a row-returning command, such as a
SELECT Statement as shown.
comAccounts.ExecuteNonQuery
ExecuteScalar
We should use this method when we want the first column of the first row of the result set returned
as shown.
'Save the number of rows in the table.
intNumRows=Cint(comAccounts.ExecuteScalar().ToString)
ExecuteXmlReader
This method is similar to the ExecuteReader method, but the returned rows must be expressed using
XML as shown.
Dim drdTest As XmlReader
drdTest=comAccounts.ExecuteXmlReader()
Fill() method - Fills the dataset with the records from a database
Update() method - Executes the corresponding InsertCommand, UpdateCommand, or
DeleteCommand for each inserted, modified, or deleted row to reflect the changes in the database.
programming. Strongly typed programming uses information from the underlying data scheme. This
means you are programming directly against your declared objects and not the tables you are really
trying to manipulate. A typed dataset has a reference to an XML schema file. This schema file (*.xsd)
describes the structure of all the tables contained within the dataset.
DataSet properties
The DataSet class properties
DefaultViewManager
Returns a view of the data in the DataSet. This view can be filtered or sorted, and we can search and
navigate through it. The returned value is of data type DataViewManager.
HasErrors
Returns a Boolean value indicating if there are any errors in the rows in the dataset tables. You can
use this property before checking any of the individual tables that also have a HasErrors property.
Tables
Returns the collection of tables from the DataSet. The returned value is of data type
DataTableCollection, and it holds objects of data type DataTable. Nothing is returned if no data
tables exist.