private void BuildCache(bool closeConnection, DataRow dataRow, bool useColumnsForParameterNames)
{
// Don't bother building the cache if it's done already; wait for
// the user to call RefreshSchema first.
if ((null != _dbSchemaTable) && (!useColumnsForParameterNames || (null != _parameterNames)))
{
return;
}
DataTable schemaTable = null;
DbCommand srcCommand = GetSelectCommand();
DbConnection connection = srcCommand.Connection;
if (null == connection)
{
throw ADP.MissingSourceCommandConnection();
}
try
{
if (0 == (ConnectionState.Open & connection.State))
{
connection.Open();
}
else
{
closeConnection = false;
}
if (useColumnsForParameterNames)
{
DataTable dataTable = connection.GetSchema(DbMetaDataCollectionNames.DataSourceInformation);
if (dataTable.Rows.Count == 1)
{
_parameterNamePattern = dataTable.Rows[0][DbMetaDataColumnNames.ParameterNamePattern] as string;
_parameterMarkerFormat = dataTable.Rows[0][DbMetaDataColumnNames.ParameterMarkerFormat] as string;
object oParameterNameMaxLength = dataTable.Rows[0][DbMetaDataColumnNames.ParameterNameMaxLength];
_parameterNameMaxLength = (oParameterNameMaxLength is int) ? (int)oParameterNameMaxLength : 0;
// note that we protect against errors in the xml file!
if (0 == _parameterNameMaxLength || null == _parameterNamePattern || null == _parameterMarkerFormat)
{
useColumnsForParameterNames = false;
}
}
else
{
Debug.Assert(false, "Rowcount expected to be 1");
useColumnsForParameterNames = false;
}
}
schemaTable = GetSchemaTable(srcCommand);
}
finally
{
if (closeConnection)
{
connection.Close();
}
}
if (null == schemaTable)
{
throw ADP.DynamicSQLNoTableInfo();
}
BuildInformation(schemaTable);
_dbSchemaTable = schemaTable;
DbSchemaRow[] schemaRows = _dbSchemaRows;
string[] srcColumnNames = new string[schemaRows.Length];
for (int i = 0; i < schemaRows.Length; ++i)
{
if (null != schemaRows[i])
{
srcColumnNames[i] = schemaRows[i].ColumnName;
}
}
_sourceColumnNames = srcColumnNames;
if (useColumnsForParameterNames)
{
_parameterNames = new ParameterNames(this, schemaRows);
}
ADP.BuildSchemaTableInfoTableNames(srcColumnNames);
}