SqlHelper.FillDataset 的一个Bug

来源:互联网 时间:1970-01-01

       这两天在做数据查询的时候,在一个存储过程对多表进行查询,分别填充到强类型的 DataSet 不同的表。把存储过程写好以后,使用 SqlHelper.FillDataset 方法调用存储过程。发现的数据只有两个表填上了,而别的指定的表没有填上数据。跟踪的时候发现,DataSet 里边多了表,也就是数据实际上填到 DataSet 了只是没有填到指定的表。
       于是怀疑是DataSet 表没拖拽好。重新拖拽后,测试发现还是一样的结果。然后把存储过程改成拼接 SQL 也不行。但是如果自己使用 SqlDataAdapter 时就没问题。索性上网 google 了一下。发现原来是 Data Access Application Block 2.0 中的一个Bug(《ADO.NET中的多数据表操作浅析之读取》)。
        希望以后有人碰到同样问题 goole 一下,别瞎忙乎了。

**//// <summary>
        /// Private helper method that execute a SqlCommand (that returns a resultset) against the specified SqlTransaction and SqlConnection
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.:  
        ///  FillDataset(conn, trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));
        /// </remarks>
        /// <param name="connection">A valid SqlConnection</param>
        /// <param name="transaction">A valid SqlTransaction</param>
        /// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
        /// <param name="commandText">The stored procedure name or T-SQL command</param>
        /// <param name="dataSet">A dataset wich will contain the resultset generated by the command</param>
        /// <param name="tableNames">This array will be used to create table mappings allowing the DataTables to be referenced
        /// by a user defined name (probably the actual table name)
        /// </param>
        /// <param name="commandParameters">An array of SqlParamters used to execute the command</param>
        private static void FillDataset(SqlConnection connection, SqlTransaction transaction, CommandType commandType, 
            string commandText, DataSet dataSet, string[] tableNames,
            params SqlParameter[] commandParameters)
            if( connection == null ) throw new ArgumentNullException( "connection" );
            if( dataSet == null ) throw new ArgumentNullException( "dataSet" );
            // Create a command and prepare it for execution
            SqlCommand command = new SqlCommand();
            bool mustCloseConnection = false;
            PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );
            // Create the DataAdapter & DataSet
            using( SqlDataAdapter dataAdapter = new SqlDataAdapter(command) )
                // Add the table mappings specified by the user
                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(); 
                        // 修正Bug.
                        dataAdapter.TableMappings.Add((index>0) ? (tableName+index.ToString()):tableName, tableNames[index]);
                // Fill the DataSet using default values for DataTable names, etc
                // Detach the SqlParameters from the command object, so they can be used again
            if( mustCloseConnection )