熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

SqlHelper.FillDataset 的一個Bug

2022-06-13   來源: .NET編程 

  這兩天在做數據查詢的時候在一個存儲過程對多表進行查詢分別填充到強類型的 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();
        }


From:http://tw.wingwit.com/Article/program/net/201311/12915.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.