ADO.NET Data Access. Page 2 SQL When we interact with the datasource through ADO.NET we use the...
-
Upload
gerard-bryan -
Category
Documents
-
view
212 -
download
0
Transcript of ADO.NET Data Access. Page 2 SQL When we interact with the datasource through ADO.NET we use the...
ADO.NET Data Access
Page 2 www.tech.findforinfo.com
SQL
When we interact with the datasource through ADO.NET we use the SQL language to retrieve,modify,update information
Structured Query Language is a standard data access language used to interact with relational database
SQL Select Statement
Update Statement
Insert Statement
Delete Statement
Page 3 www.tech.findforinfo.com
SQL Select Statement
To retrieve one or more rows of data the select command is used
SELECT [columns] FROM [tables] WHERE [search_condition]ORDER BY [order_expression ASC | DESC]
Page 4 www.tech.findforinfo.com
Sample Select Statement
(eg) SELECT * FROM Authors (*) - retrieves all the columns from the table
The FROM clause identifies that the Author table is being used
There is no WHERE clause, means all the records will be retrieved
There is no ORDER BY clause, means the data’s are not sorted
Page 5 www.tech.findforinfo.com
Improving the Select Statement
SELECT lname,fname FROM Authors WHERE State =‘MI’ ORDER BY lname ASC
Only two columns are retrieved
WHERE Clause provides the restriction to a particular state
An ORDER BY clause sorts the lname alphabetically in ascending order
Page 6 www.tech.findforinfo.com
The WHERE clause
(eg)SELECT * FROM Sales WHERE ord_date<‘2000/01/01’ AND ord_date> ‘1987/01/01’
Multiple conditions can be combined using the AND keyword
Greater than and Less than comparisons can be made using <,> symbols
Page 7 www.tech.findforinfo.com
SQL Update Statement
Syntax:
Eg UPDATE Authors SET lname=‘Gowri’ WHERE id =‘101’
UPDATE [table] SET [update_epression] WHERE [search_condition]
Page 8 www.tech.findforinfo.com
SQL Insert Statement
Syntax
Eg INSERT INTO Authors (lname,fname) VALUES (‘John’,’Khan’)
INSERT INTO [table] ([column_list]) VALUES ([value_list])
Page 9 www.tech.findforinfo.com
The SQL Delete Statement
Syntax
Eg DELETE FROM Authors WHERE id=‘102
DELETE FROM [table] WHERE [search_condition]
Page 10 www.tech.findforinfo.com
Simple Data Access
To retrieve information with simple Data Access follow these steps
Create Connection,Command,DataReader objects
Use DataReader to retrieve information from the database and display it in a control in the webform
Close your connection
Send the page to the user.At this point all the connections have been closed
Page 11 www.tech.findforinfo.com
Importing Namespaces
Imports System.Data
Imports System.Data.OleDb
(OR) Imports System.Data.SqlClient
Page 12 www.tech.findforinfo.com
Creating a connection
The first step is to make a connection with the database
Specify a value for the ConnectionString property
This property defines all the information the computer needs to find the data source,log in and initial database
Page 13 www.tech.findforinfo.com
The Connection String
The ConnectionString is actually a series of distinct pieces of
information,separated by semicolons(;)
Provider –This refers to the name of the Provider which allows
communication between ADO.NET and database (SQLOLEDB is the
provider for SQL)
Data Source – This refers to the name of the server where the data
source is located;Server is on the same computer so localhost
Page 14 www.tech.findforinfo.com
The Connection String
Initial CataLog – It refers to the name of the initial database
User ID – Used to access the database ,sa refers to the system admin
account
Password - By default the ‘sa’ account does not have a password
ConnectionTimeout – It determines how long your code will wait in
seconds before generating an error if it cannot establish connection
default 15 seconds
Page 15 www.tech.findforinfo.com
SQl Server Integrated Authentication
With SQL Server Authentication
SQL Server maintains its own user account
With integrated authentication
SQL Server automatically uses the windows account information for the currently logged in user
Page 16 www.tech.findforinfo.com
Making the connnection
Page 17 www.tech.findforinfo.com
Defining a select command
The connection object a few basic properties that give the information about the connection
An SQL Statement that selects the information you want
(eg) SELECT * from Authors
A command object that executes the SQL statement
SqlDataAdapter adap=new SqlDataAdapter(“SELECT * from Author”,con)
A DataSet object to store the retrieved objects
(eg)DataSet ds=new DataSet()