 |
| 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
|
|
|
|

June 1st, 2004, 10:18 AM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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()
***
|
|

June 1st, 2004, 12:16 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hello,
The SQLDataAdapter handles the opening and closing of the connection. You don't need to do anything with the connection manually.
Brian
|
|

June 1st, 2004, 03:26 PM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

June 1st, 2004, 04:05 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Hello,
Yep, that about sums it up. I'm not sure the reasoning for the architecture, but that's what happens.
Brian
|
|

June 1st, 2004, 04:07 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|
|

June 1st, 2004, 11:48 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
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.
|
|
 |