调用SqlHelper中的FillDataSet方法,来填充DataSet中的多个DataTable,使用一个数组来对多个DataTable命名,如下:
string
[] tableNames
=
new
string
[]
{"BLE_LISTINGS","BLE_LISTINGS1","BLE_LISTINGS2"}
;
前两个DataTable运行正确,TableName分别为“BLE_LISTINGS"和"BLE_LISTINGS1",但是第三个DataTable的TableName为Table2,百思不得其解,没办法,只好仔细看看SqlHelper的代码,发现是如下代码造成了该错误:
if
(tableNames
!=
null
&&
tableNames.Length
>
0
)
{
string tableName = "Table";
for (int index=0; index < tableNames.Length; index++)
{
if( tableNames[index] == null || tableNames[index].Length == 0 ) throw new ArgumentException( "The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames" );
dataAdapter.TableMappings.Add(tableName, tableNames[index]);
tableName += (index + 1).ToString();//这句代码造成了该错误
}
}
{
string tableName = "Table";
for (int index=0; index < tableNames.Length; index++)
{
if( tableNames[index] == null || tableNames[index].Length == 0 ) throw new ArgumentException( "The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames" );
dataAdapter.TableMappings.Add(tableName, tableNames[index]);
tableName += (index + 1).ToString();//这句代码造成了该错误
}
}
因为TableName是string类型,所以当index递增的时候,Table1之后便是Table12而不是作者期望得到的Table2,本来打算直接修改其代码,但是又生怕其代码有关联性,要是由于我的改动再造成其他地方的错误,那就太麻烦了,于是到 patterns & practices: Data Access Application Block & Guide: Workspace Home看了一下,发现在 Bug Tracker 中早已有该Bug的存在 ,作者亦提供了修复该bug的解决方案。其实也就是把那句代码改了。修改后的代码如下:
string
tableName
=
"
Table
"
;
for ( int index = 0 ; index < tableNames.Length; index ++ )
{
if( tableNames[index] == null || tableNames[index].Length == 0 )
throw new ArgumentException( "The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames" );
dataAdapter.TableMappings.Add(
tableName + (index == 0 ? "" : index.ToString()),
tableNames[index] );
}
for ( int index = 0 ; index < tableNames.Length; index ++ )
{
if( tableNames[index] == null || tableNames[index].Length == 0 )
throw new ArgumentException( "The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames" );
dataAdapter.TableMappings.Add(
tableName + (index == 0 ? "" : index.ToString()),
tableNames[index] );
}
提醒自己以后在使用第三方组件的时候一定要先看Bug Tracker!