SqlHelper.FillDataset 的一个 Bug

今天在做项目时,想用SqlHelper下的FillDataset 这个方法,要网上查了下,说这个里面有bug,所以就记下来

这两天在做数据查询的时候,在一个存储过程对多表进行查询,分别填充到强类型的 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 
                dataAdapter.Fill(dataSet); 
 
                
// Detach the SqlParameters from the command object, so they can be used again 
                command.Parameters.Clear(); 
            }
 
 
            
if( mustCloseConnection ) 
                connection.Close(); 
        }
 

 //  错误处.
                        
// dataAdapter.TableMappings.Add(tableName, tableNames[index]);
                        
// tableName += (index + 1).ToString(); 
                        
//
                        
//  修正Bug.
                        dataAdapter.TableMappings.Add((index > 0 ?  (tableName + index.ToString()):tableName, tableNames[index]);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值