Data Access Application Block for .NET v2.0 中的一个小问题

本文探讨了在使用.NET v2.0的DataAccessApplicationBlock中,SqlHelper类的FillDataset方法在填充包含两个以上表的数据集时出现的表映射错误。通过修改dataAdapter.TableMappings.Add方法的参数传递方式,成功解决了这一问题。

看了SqlHelper类的FillDataset方法,当用SqlDataAdapter填充数据到数据集(包括强类型数据集)时,如果超过二个表,就会出现表映射错误。
  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();
     }
    }
上面的
      dataAdapter.TableMappings.Add(tableName, tableNames[index]);
修改成
dataAdapter.TableMappings.Add((index>0)?(tableName+index.ToString()):tableName, tableNames[index]);
就行了。

posted on 2004-07-15 15:14  秋枫 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/zhzuo/archive/2004/07/15/24494.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值