• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
Introduction to ADO.NET
Introduction
This is a set of questions and answers related to ADO.NET. All the code is in
Beta 2.
The questions by no means cover the entire capabilities of ADO.NET. Specifically, Ihave tried to answer following questions :
What is ADO.NET?
Is ADO.NET different than ADO?
What are managed providers?
What are the data access namespaces in .NET?
What are main objects in ADO.NET ?
How do I establish connection with a database?
How do I use Command object to execute action queries?
How do I use command object to execute Select queries?
How do I use command object to execute stored procedure?
What is DataAdapter and Dataset?
How do i populate a dataset?
How do I navigate dataset?
How do I add new record?
How do I change data ?
How do I delete a row?
How do I filter data once arrived in data set?
How do I handle errors ?
How do I handle transactions?
What is ADO.NET?
ADO.NET is next generation of data access technology fromMicrosofttarget at .NETplatform. ADO.NET is built with distributed and internetapplicationsin mind.ADO.NET provides strong support for XML and disconnected data processing.
Is ADO.NET different than ADO?
ADO.NET is much different than ADO. In order to achieve disconnected data accessprogrammers have to use different techniques like disconnected recordsets, RDS etc.ADO object model is very small as compared to ADO.NET. ADO.NET provides numberof specialized objects to handle very specific tasks. Microsoft has taken care toclosely map properties and methods of ADO.NET objects with existing ADOcounterparts. As per Microsoft ADO.NET is not a replacement for ADO but anenhancement in the overall data access technology. You can use both ADO andADO.NET in your application.
What are managed providers?
A managed provider is analogous to ODBCdriveror OLEDB provider. It performsoperation of communicating with the database. ADO.NET currently provides twodistinct managed providers. The SQLServermanaged provider is used with SQLserver and is a very efficient way of communicating with SQL Server. OLEDBmanaged provider is used to communicate with any OLEDB compliant database like
 
Access or Oracle. The data accessAPIsfor both the providers are found in separatenamespaces.
What are the data access namespaces in .NET?
Following are the most common data access namespaces :
System.Data
System.Data.OleDb
System.Data.SQLClient
System.Data.SQLTypes
System.Data.XML
What are main objects in ADO.NET?
Following are the main objects in ADO.NET :
OleDbConnection / SQLConnection
OleDbCommand / SQLCommand
OleDbDataReader / SQLDataReader
OleDbDataAdapter / SQLDataAdapter
OleDbParameter / SQLParameter
DataSet
DataTable
DataView
DataRow
DataColumnMost of the objects mentioned above can be created as 'stand-alone' objects viacode. In the discussion below we will see all the examples with OLEDB objects. Theusage for SQL server objects is same except name changes. Also, for simplicity Ihave omitted the repetitive code (like opening a database connection) in laterexamples.
How do I establish connection with a database?
The way you connect to a database is very similar to that used in ADO. You useconnection object to accomplish this. Consider following code fragment :
Dim cnn as OleDbConnectioncnn=new OleDbConnection("OLEDB_connection_string")cnn.open()'use connection herecnn.close()
How do I use Command object to execute action queries?
Using command object is similar to that from ADO.
 
Dim cmd as OleDbCommandDim cnn asOleDbConnectioncnn=new OleDbConnection("OLEDB_connection_string")cnn.open()cmd=newOleDbCommand("my_action_query",cnn)cmd.ExecuteNonQuery()cmd.CommandText="new_query"
How do I use command object to execute Select queries?
You can collect records returned by a command object using DataReader object.
Dim dr as OleDbDataReaderDim cmd as OleDbCommandDim cnn as OleDbConnectioncnn=new OleDbConnection("OLEDB_connection_string")cnn.open()cmd=new OleDbCommand("my_select_query",cnn)dr=cmd.ExecuteReader()do while dr.read()response.write(dr("field1"))loopdr.close()
How do I use command object to execute stored procedure?
You can execute a stored procedure in the same way as any query. Followingexample shows how to execute parameterize stored procedure.
Dim cmd as OleDbCommandDim p as OleDbParameterDim cmd as OleDbCommandDim cnn as OleDbConnectioncnn=new OleDbConnection("OLEDB_connection_string")cnn.open()
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...