Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 1st, 2004, 10:18 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default Connection Object


Code below works fine without opening a connection object
explicitly. I mean objConn.Open() Why?

**
Dim objConn As New SqlConnection ("server=localhost;uid=sa;pwd=;database=pubs;")

        Dim strSQL As String = "SELECT * FROM Authors"
        Dim objCommand As New SqlCommand(strSQL, objConn)

        Dim objDA As New SqlDataAdapter
        objDA.SelectCommand = objCommand

        Dim objDS As New DataSet
        objDA.Fill(objDs)

        dgAuthors.DataSource = objDS
        dgAuthors.DataBind()
***
 
Old June 1st, 2004, 12:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

The SQLDataAdapter handles the opening and closing of the connection. You don't need to do anything with the connection manually.

Brian
 
Old June 1st, 2004, 03:26 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Brian,
I migrating from classic asp to asp.net

Please correct me, if i have understand your answer

When you use SQLDataAdapter object it handles opening and closing of the connection implicitly, but when you use command object (executenonquery() or executereader() ) you have
to open and close your connection in your code explicitly.

Thanks in advance.
 
Old June 1st, 2004, 04:05 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

Yep, that about sums it up. I'm not sure the reasoning for the architecture, but that's what happens.

Brian
 
Old June 1st, 2004, 04:07 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

I probably should also explain that the SQLDataReader object requires an active connection to the database, as well as executing with no results or a scalar value. It requires a connected environment.

In addition, the SQLDataAdapter passing to the DataSet/DataTable doesn't require an active connection when reading/writing to the objects, and is therefore in a disconnected environment.

Hope that helps,

Brian
 
Old June 1st, 2004, 11:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

DataAdapter is new in .NET! so powerfull Obj to work with DataBase & DataSet. It has contained 4 Objects for SELECT, Insert, Update & Delete. but u work much with 2 miracle Methods of that for all of there 4. [u]DataAdapter.fill(DataSet)</u> means SELECT Data from DB & fill ur mentioned DatSet, ofter that what ever u do on ur DataSet will need [u]DataAdapter.Update(DataSet)</u> if u have deleted 3 Record of ur DataSet & Updated 2 Record & Inserted 5 Row in DataSet they all will Updated with DB with a SINGLE Update Method. fo rmor read ur MSDN & find out what a great gift is! but in other hand has some disadvantaged. Like working Client side as a traditional ADO obj.

Always:),
Hovik Melkomian.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection String (Object) ssimkhan Visual Basic 2005 Basics 1 June 1st, 2007 10:41 AM
Connection String(object) ssimkhan Visual Studio 2005 1 May 31st, 2007 04:54 PM
Global Connection Object sarahmapg ADO.NET 1 May 18th, 2005 06:00 AM
Database Connection Object mrideout BOOK: Beginning ASP.NET 1.0 4 August 3rd, 2004 07:42 AM
Connection Object question Kenny Alligood VB Databases Basics 5 July 15th, 2004 07:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.