Tuesday, August 30, 2016

More DataAccess - And You Thought I Was Done With It!

Back in 2009, I wrote a 3-part series of blog posts about DataAccess. You can find them here: Part I, Part II and Part III.  These posts are still relevant, even though they are old. Then, a year ago, I revisited that topic to add some additional things to my DataAccess class (implementing IDisposable and Transactions with TransactionScope). You can find that here: DataAccess Revisited

And now, I need to add something else that I’ve been meaning to add, because my DataAccess class is missing something important!! It was pointed out to me by a few people (via email) probably also about a year ago … it just took me awhile to get back to this. I hope it haven’t negatively affected too many people (probably not, or I would have had more complaints about it … or people figured it out themselves, all the while cussing me out silently)!!!

Anyway, that missing thing is TableMappings on the DataAdapter!! Basically, this needs to be added to the BBDataAccess class:

private void SetTableMappings(DataSet ds)
{
if (this.oAdapter.TableMappings.Count > 0)
return;

DataTable dt;
string TableName;
for (int i = 0; i < ds.Tables.Count; i++)
{
dt = ds.Tables[i];
if (dt.TableName.ToUpper().StartsWith("TABLE") == false)
{
if (i == 0)
TableName = "Table";
else
TableName = "Table" + i.ToString();
this.oAdapter.TableMappings.Add(TableName, dt.TableName);
}
}
}

And the existing FillData() method needs to have a call to SetTableMappings() before filling the DataSet, like this:

protected void FillData(DataSet ds, string StoredProcName)
{
this.oCommand.CommandText = StoredProcName;
this.SetTableMappings(ds);
this.oAdapter.Fill(ds);
}

That should take care of it! 

Happy Coding!  =0)