新发现:微软提供的SqlHelper中FillDataset方法一个小Bug
FillDataset(SqlConnection connection, CommandType commandType,
string commandText, DataSet dataSet, string[] tableNames)
当调用此方法,并指定映射表名数组,对应参数tableNames
若元素超过3个,即指定表名超过3个时,从第3个表名开始返回的将是系统默认的表名
而非用户指定的表名(如,指定"header" "detail" "relation",第3个返回了Table2)
此Bug原因在这里:
private static void FillDataset(SqlConnection connection, SqlTransaction transaction, CommandType commandType,
string commandText, DataSet dataSet, string[] tableNames,
params SqlParameter[] commandParameters)
{
...
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(); //若超过3个表,则
tableName会变成Table11,Table111,返回映射表名即为Table2,Table3了
解决方法即在此句前面加“tableName = "Table";”即可
}
...
}
若有说错地方,请高手指正,多谢!