A few misc tricks with ADO.NET 2.0

Published 14 July 4 12:43 AM | William

I also have this posted at http://www.knowdotnet.com/articles/miscadonet.html  (with a lot better formatting ;-))

Eariler tonight I started playing with some more ADO.NET 2.0 code and just started mixing it up. It appears the ASP.NET 2.0 team has done such a good job with ASP.NET 2.0 that you don't really need to write any code for it anymore, so I can spend my time elsewhere. There are two new ADO.NET 2.0 Features that are pretty cool - although I probably won't be using the first much:

Generic DB Providers:

private DbConnection cn;

private void button1_Click(object sender, EventArgs e)

{

DbProviderFactory myFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");

DbDataSourceEnumerator eNum = myFactory.CreateDataSourceEnumerator();

DataTable providers = eNum.GetDataSources();

dgv.DataSource = providers;

//At this point, it shows Bill_2k3 - the only instance of SQL Server

//Running internally on my network

using (cn = myFactory.CreateConnection())

{

cn.ConnectionString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=xxxxxxx";

try

{

cn.Open();

DbCommand cmd = myFactory.CreateCommand();

cmd.Connection = cn;

cmd.CommandText = "SELECT * FROM Customers";

DbDataReader dr = cmd.ExecuteReader();

}

//Notice we're catching a Generic DBException object

catch (DbException ex)

{

Debug.Assert(false, ex.ToString());

}

finally {

cn.Close();

}

}

}

New DataReader Functionality:

As you can see, it's a pretty generic impelementation. The only thing that's really hard coded is the GetFactory method, but that's just b/c I didn't have a UI that I wanted to use. I could have selected something out of a grid or ListView for instance, passed it into a method and used that value as a parameter.

Two things come to mind. One is that connection strings vary from provider to provider so you do need to know a little about what you are using. The next is that you don't have very limited functionality. For instance, there are NO events other than .Dispose for DbConnection. That's not surprising but it's worth mentioning - after all, how would Access implement the InfoMessage event when it's specific to SqlServer?

The Next neat piece is the ability to populate a DataTable from a DataReader. Now, you may be aware that the DataAdapter objects use DataReaders behind the scenes to fill DataTables/DataSets, so what' the big deal? Well, you don't need an Adapter at all to populate a DataTable. For read only situation where you need an IEnumerable object for instance, you can take advantage of the performance benefits and flexibilty associated with this:

private DbConnection cn;

private void button1_Click(object sender, EventArgs e)

{

DbProviderFactory myFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");

DbDataSourceEnumerator eNum = myFactory.CreateDataSourceEnumerator();

DataTable providers = eNum.GetDataSources();

dgv.DataSource = providers;

//At this point, it shows Bill_2k3 - the only instance of SQL Server

//Running internally on my network

using (cn = myFactory.CreateConnection())

{

DbConnectionStringBuilder csb = new DbConnectionStringBuilder(;

cn.ConnectionString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind";

try

{

cn.Open();

DbCommand cmd = myFactory.CreateCommand();

cmd.Connection = cn;

cmd.CommandText = "SELECT * FROM Customers";

DbDataReader dr = cmd.ExecuteReader();

//cn.Close();

DataTable dt = new DataTable("BillsTest");

Debug.WriteLine(dt.Rows.Count.ToString());// Returns 0 proving it's empty

dt.Load(dr);

Debug.WriteLine(dt.Rows.Count.ToString());// Returns 91

//That's right, we just filled a DataTable without a DataAdapter and without iterating through

//something to fill it.

DbDataReader drTwo = dt.GetDataReader();

Debug.Assert(drTwo.HasRows);

while (drTwo.Read())

{

lb.Items.Add((string)drTwo.GetString(0));

Debug.WriteLine(lb.Items.Count.ToString());// Returns 91

}

Debug.WriteLine(dt.Rows.Count.ToString());// Returns 91

}

//Notice we're catching a Generic DBException object

catch (DbException ex)

{

Debug.Assert(false, ex.ToString());

}

cn.Close();

}

}

I think what I'm doing here is pretty straightforward so I'm not going to go into too much depth. Basically, you can turn a DataTable into a DataReader or vice versa. The .Load() method takes a DataReader object and loads it into a DataTable. .GetDataReader does the exact opposite. Notice though that I commented out the .Close() command right after ExecuteReader. If I don't do this, everything will still work, at least in the sense of no Exceptions. However you still need an open connection if you want data back from the db. So by uncommenting that line, the program will still run, it just won't load any data into the listbox. I'm a bit uncomfortable with this b/c it could be easy to overlook - God knows I've done it with DataReaders enough in the earlier versions of the framework and the exception was the main thing that let me know about it.

Filed under:

Comments

# William said on December 7, 2004 1:46 AM:

I was not able to find the method .Load in DataTable..Cud let me know as how you have accomplished the DT and DR inter swaps..

# William said on January 13, 2005 6:02 AM:

You may find it interesting to check some relevant pages in the field of poker http://www.middlecay.org/
online poker http://www.hasslerenterprises.org/
phentermine http://www.teambeck.org/
viagra http://www.neweighweb.org/
casino http://www.hdic.org/
online casino http://www.hometeaminspection.org/
texas holdem http://www.mor-lite.org/
texas hold em http://www.reservedining.org/
pacific poker http://www.paramountseedfarms.org/
empire poker http://www.parkviewsoccer.org/
party poker http://www.tclighting.org/
cialis http://www.sportingcolors.org/
diet pills http://www.psychexams.org/
tramadol http://www.stories-on-cd.org/
play poker http://www.marshallyachts.org/
turbo tax http://www.lvcpa.org/
pacific poker http://www.rifp.org/
texas hold em poker http://www.suttonjames.org/
poker rules http://www.pagetwo.org/
credit cards http://www.tecrep-inc.org/
hoodia http://www.rethyassociates.org/
carisoprodol http://www.ingyensms.org/
payday loans http://www.krantas.org/
buy phentermine http://www.devilofnights.org/
generic viagra http://www.ansar-u-deen.org/
h r block http://www.azian.org/
buy viagra http://www.twinky.org/
texas holdem poker http://www.atlanta2000.org/
weight loss diet pills http://www.catchathief.org/
weight loss http://www.nehrucollege.org/
casino http://www.casinoequipmentsalesandrental.com/
... Thanks!!!

Search

This Blog

Tags

Community

Archives

News

  • William G Ryan William Ryan Bill Ryan W.G. Ryan Charles Mark Carroll Charles M Carroll
    My Blog Juice Microsoft MVP
    Bill Ryan W.G. Ryan William Ryan
    Cuckooz' MySpace Page View Bill Ryan's profile on LinkedIn
    My Profile on Twitter
    Please note that this is my personal blog and the opinions expressed are my own. Also, comment moderation is about one of the least important things in my life so please keep that in mind. I can't vouch for the authenticity of any of the posters so please don't hold me accountable. And whatever you do, don't pretend to be Noted Option Strict Off expert and AspFriend Charles Mark Carroll when you post. Doing so will lead him to become apoplectic and write absurd accusatory posts about me that are as coherent and thought out as they are factually correct. He does a stellar job proving his reputation is well deserved and he doesn't need any help from you making himself look foolish. If I have to listen to him banging his spoon off of his high chair one more time, I'm going to burst into flames so please don't make that happen!

    My other sites

    Cool Stuff

    Book Stuff

    Security

    ORM

    Data Access

    Funny Stuff

    Compact Framework Stuff

    Web Casts

    My KnowledgeBase Articles

    My MVP Profile

    Design Patterns

    Performance

    Debugging

    Remoting

    My Fellow Authors

    My Books

    LINQ

    Misc

    Speech

    Syndication

    Email Notifications