Question Database Connection Method

dfenton21

Member
Joined
Apr 26, 2011
Messages
20
Programming Experience
Beginner
I have a newbie question.

Is there any difference between connecting to a database using the "Add Connection" wizard and manually typing the code, i.e,

Dim con As New OleDbConnection
Dim ds As New DataSet
Dim dt As New DataTable
Dim da As New OleDbDataAdapter

con.ConnectionString = ConnString
con.Open()

ds.Tables.Add(dt)

da = New OleDbDataAdapter(SQLString, con)

da.Fill(dt)

ReturnDataTable = (dt)

con.Close()

Is one method faster than the other?


Thanks.
 
The wizard you mention has nothing to do with your application. It adds a connection to the Server/Database Explorer. The "equivalent" to your code would be the Data Source wizard. Part of the Data Source wizard is selecting/creating a connection from which to get the schema. Any connections you add using the Add Connection wizard will be listed in a drop-down for fast selection.

When using the Data Source wizard, one option is to generate a typed DataSet. In doing so, you generate various classes that derive from the more general classes you would use if you wrote your own ADO.NET code, as in your example. A class is generated that inherits the DataSet class. This class has a property for each specific DataTable, rather than your getting a table by name or index from the Tables collection. The advantage of that is that you get full Intellisense support. There's no chance of your trying to get a table that doesn't exist by supplying the wrong name of index. Similarly, specific classes are generated for each DataTable and DataRow that have members specific to your data. This generally makes working with the data easier, mainly courtesy of Intellisense but also because there's no casting required as you get data out of the rows.

TableAdapter classes are also generated; one per DataTable. Each TableAdapter doesn't inherit the DataAdapter class, but it does wrap one. Instead of having to create a connection and an adapter, supplying the connection string and the SQL code, all that is done internally by the TableAdapter. You simply create one and call Fill or Update.

if you want to use DataTables then there's pretty much no reason to not use the Data Source wizard to generate a typed DataSet. Many people will tell you otherwise but those people don't understand what a typed DataSet can and can't do. Lots of people say that they prefer to write all their ADO.NET code by hand because it gives them more control. They are wrong. You have complete control over a typed DataSet. There are limitations on what the wizard can generate but you are free to make all the modifications you want in the DataSet designer. You can even construct arbitrary SQL code at run time and use it in a TableAdapter if you really want to, although it would be fairly pointless to do so. That would be the one area where I would definitely use code like yours, although not because a typed DataSet couldn't handle it.
 
Back
Top