Add these new methods in DAAB if using strongly typed dataset.

Topics: Data Access Application Block
Nov 29, 2007 at 6:38 PM
There is a problem in DAAB in Enterprise Library 3.1. I want to use a strongly typed dataset to store the data retrieved from an Oracle returning cursor, if element names in datatable in the dataset is different than column names in the cursor, I cannot use Dabase.LoadDataSet method to populate data into the data table because there is no column mapping information. In order to do so, I have to add the following code in DataBase.cs and OracleDataBase.cs:

(1) Add these two methods in DataBase.cs.

public virtual void LoadDataSet(DbCommand command, DataSet dataSet, DataTableMapping tableMapping)
{
using (ConnectionWrapper wrapper = GetOpenConnection())
{
PrepareCommand(command, wrapper.Connection);
DoLoadDataSet(command, dataSet, tableMapping);
}
}


private void DoLoadDataSet(DbCommand command, DataSet dataSet, DataTableMapping tableMapping)
{

using (DbDataAdapter adapter = GetDataAdapter(UpdateBehavior.Standard))
{
((IDbDataAdapter)adapter).SelectCommand = command;

try
{
DateTime startTime = DateTime.Now;

dataSet.EnforceConstraints = false;
adapter.TableMappings.Clear();

tableMapping.SourceTable = "Table";
adapter.MissingMappingAction = MissingMappingAction.Ignore;
adapter.TableMappings.Add(tableMapping);


adapter.Fill(dataSet);
instrumentationProvider.FireCommandExecutedEvent(startTime);
}
catch (Exception e)
{
instrumentationProvider.FireCommandFailedEvent(command.CommandText, ConnectionStringNoCredentials, e);
throw;
}
}
}

(2) Add this method into OracleDatabase.cs.

public override void LoadDataSet(DbCommand command, DataSet dataSet, DataTableMapping tableMapping)
{
PrepareCWRefCursor(command);
base.LoadDataSet(command, dataSet, tableMapping);
}


This solution has been tested and now the data can be populated into the datatable in the strongly typed dataset, even though element names in datatable is totally different than column names in Oracle tables.

I suggest these 3 pieces of code can be added into DAAB with the future version of Enterprise Library. Otherwise, our company won't use DAAB for data access.