Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DbCommandBuilder and DataAdaptor vs DataReader

Status
Not open for further replies.

tfstom

Programmer
Sep 28, 2002
190
US
I have different books that I am using to play with ASP.NET and one talks about the DataAdapter, dbCommandBuilder and DataSet object - but doesn't mention the DataReader object. The other book talks and uses the DataReader object and doesn't mention the other 3.

Why is this? Are they the same, just different ways of accomplishing the same thing? Does it matter which you use?

For example the following 2 sets of code, apparently accomplish the same thing. It seems to be easier to use the DataReader style. But if the other style gives you something better than the DataReader, you might want to use the other.

Code:
Dim ConnectString, SelectStatement As String
Dim Connect As OleDbConnection = New OleDbConnection
Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter
Dim ClassyCB As OleDbCommandBuilder
Dim ClassyDS As DataSet = New DataSet
Dim Row As DataRow

SelectStatement = "Select * From Ads Where AdNum=" & _
   AdNumSent
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
   "Data Source=c:\inetpub\[URL unfurl="true"]wwwroot\simplyClassy\classydb.mdb"[/URL]
Connect.ConnectionString = ConnectString
Adapter.SelectCommand = _
   new OleDbCommand(SelectStatement, Connect)
' ClassyCB = New OleDbCommandBuilder(Adapter)
Adapter.SelectCommand.Connection.Open
Adapter.Fill(ClassyDS,"Ads")
Row = ClassyDS.Tables("Ads").Rows(0)
TitleText.Text = Row.Item("Title")

Code:
Dim ConnectString, SelectStatement As String
Dim Connect As OleDbConnection = New OleDbConnection
Dim dbCommand As OleDBCommand

SelectStatement = "Select * From Ads Where AdNum=" & _
   AdNumSent
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
   "Data Source=c:\inetpub\[URL unfurl="true"]wwwroot\simplyClassy\classydb.mdb"[/URL]
Connect.ConnectionString = ConnectString
dbCommand = New OleDbCommand(SelectStatement)
dataResponse = dbCommand.ExecuteReader()

TitleText.Text = dataResponse("Title")
 
tf: one arguement would lie in the fact that the reader class is 'more efficient' at one way reading of data than an adapter is, and therefore would be the object of choice if you made a lot of data request per postback, e.g. paging in a datagrid.

A DataAdapter populates a DataSet structure, which provides you with the main tools for working with data outside of the database table.

So the primary difference, as I see it, is functionality -- raw data, hit tables often, DataReader is preferred - working with DataSets, manipulatig, sorting data, then use the DataAdapter.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top