数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

SqlHelper.FillDataset 的一个Bug


发布日期:2021年02月26日
 
SqlHelper.FillDataset 的一个Bug

这两天在做数据查询的时候在一个存储过程对多表进行查询分别填充到强类型的 DataSet 不同的表把存储过程写好以后使用 SqlHelperFillDataset 方法调用存储过程发现的数据只有两个表填上了而别的指定的表没有填上数据跟蹤的时候发现DataSet 里边多了表也就是数据实际上填到 DataSet 了只是没有填到指定的表

于是怀疑是DataSet 表没拖拽好重新拖拽后测试发现还是一样的结果然后把存储过程改成拼接 SQL 也不行但是如果自己使用 SqlDataAdapter 时就没问题索性上网 google 了一下发现原来是 Data Access Application Block 中的一个Bug(《ADONET中的多数据表操作浅析之读取》)

希望以后有人碰到同样问题 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>

/// eg:

/// FillDataset(conn trans CommandTypeStoredProcedure GetOrders ds new string[] {orders} new SqlParameter(@prodid ));

/// </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 TSQL 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 && tableNamesLength > )

{

string tableName = Table;

for (int index=; index < tableNamesLength; index++)

{

if( tableNames[index] == null || tableNames[index]Length == ) throw new ArgumentException( The tableNames parameter must contain a list of tables a value was provided as null or empty string tableNames );

//

// 错误处

//dataAdapterTableMappingsAdd(tableName tableNames[index]);

//tableName += (index + )ToString();

//

// 修正Bug

dataAdapterTableMappingsAdd((index>) ? (tableName+indexToString()):tableName tableNames[index]);

}

}

// Fill the DataSet using default values for DataTable names etc

dataAdapterFill(dataSet);

// Detach the SqlParameters from the command object so they can be used again

commandParametersClear();

}

if( mustCloseConnection )

connectionClose();

}

               

上一篇:数据访问-DbConnectionStringBuilder

下一篇:LINQ to SQL语句之Insert/Update/Delete操作