ADO.NET 2.0 Quickies#

Writing data access code will probably remain useful for quite some time, especially since ObjectSpaces is being rolled into Longhorn's WinFS data store. I sure do understand why, but I'm still a little sad to see it go (although it's not really gone of course).

Anyway, here's a quick dump of some things I picked up from a recent MSDN TV episode on ADO.NET 2.0. Especially cool is the "provider-agnostic data access code" so you're never coding against an actual provider (SQL Server, Oracle, ODBC, plain text, whatever, ...) but use the generic versions through a factory...

// Get the configured providers if you want to see them all.
DataTable providers = DbProviderFactories.GetFactoryClasses();
// The "InvariantName" column contains the invariant name to be passed to GetFactory.

// Use a specific provider.
DbProviderFactory factory = DbProviderFactories.GetFactory( invariantName );
using( DbConnection c = factory.CreateConnection() )
{
    // Use generic methods to create commands and other ADO.NET goodies.
    c.ConnectionString = "...";
    DbCommand cmd = c.CreateCommand();
    cmd.CommandText = "...";

    // Something else that's new: load a DataTABLE directly in stead of a DataSET.
    DbDataReader r = cmd.ExecuteReader();
    DataTable table = new DataTable();
    table.Load( r );
}

// And for perfomance, in stead of updating each row separately to the DB,
// batch them all at once to lower the number of connections to the DB.
// This will call sp_executesql(""); with a sql string that contains the batched statements.
DataAdapter da; // Initialize this...
da.UpdateBatchSize = 100;

Blog | Programming | .NET | ASP.NET | Whidbey | Windows | Longhorn
Comments are closed.
All content © 2012, Jelle Druyts
On this page

Recent Photos
www.flickr.com
This is a Flickr badge showing public photos from Jelle Druyts. Make your own badge here.
Advertising
Top Picks
Statistics
Total Posts: 350
This Year: 0
This Month: 0
This Week: 0
Comments: 530
Archives
Sitemap
Disclaimer
This is my personal website, not my boss', not my mother's, and certainly not the pope's. My personal opinions may be irrelevant, inaccurate, boring or even plain wrong, I'm sorry if that makes you feel uncomfortable. But then again, you don't have to read them, I just hope you'll find something interesting here now and then. I'll certainly do my best. But if you don't like it, go read the pope's blog. I'm sure it's fascinating.

Powered by:
newtelligence dasBlog 2.0.7226.0

Sign In