• DataReader转DataTable,DataReader转DataSet


    转载

    /// <summary>
            /// DataReader转DataTable
            /// </summary>
            /// <param name="dataReader"></param>
            /// <returns></returns>
            public static DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
            {
                ///定义DataTable
                DataTable datatable = new DataTable();
    
                try
                {    ///动态添加表的数据列
                    for (int i = 0; i < dataReader.FieldCount; i++)
                    {
                        DataColumn myDataColumn = new DataColumn();
                        myDataColumn.DataType = dataReader.GetFieldType(i);
                        myDataColumn.ColumnName = dataReader.GetName(i);
                        datatable.Columns.Add(myDataColumn);
                    }
    
                    ///添加表的数据
                    while (dataReader.Read())
                    {
                        DataRow myDataRow = datatable.NewRow();
                        for (int i = 0; i < dataReader.FieldCount; i++)
                        {
                            myDataRow[i] = dataReader[i].ToString();
                        }
                        datatable.Rows.Add(myDataRow);
                        myDataRow = null;
                    }
                    ///关闭数据读取器
                    dataReader.Close();
                    return datatable;
                }
                catch (Exception ex)
                {
                    ///抛出类型转换错误
                    //SystemError.CreateErrorLog(ex.Message);
                    throw new Exception(ex.Message, ex);
                }
    
            }
            /// <summary>
            /// DataReader转DataSet
            /// </summary>
            /// <param name="reader"></param>
            /// <returns></returns>
            public static DataSet ConvertDataReaderToDataSet(SqlDataReader reader)
            {
                DataSet dataSet = new DataSet();
                do
                {
                    // Create new data table
                    DataTable schemaTable = reader.GetSchemaTable();
                    DataTable dataTable = new DataTable();
                    if (schemaTable != null)
                    {
                        // A query returning records was executed 
                        for (int i = 0; i < schemaTable.Rows.Count; i++)
                        {
                            DataRow dataRow = schemaTable.Rows[i];
                            // Create a column name that is unique in the data table 
                            string columnName = (string)dataRow["ColumnName"]; //+ " // Add the column definition to the data table 
                            DataColumn column = new DataColumn(columnName, (Type)dataRow["DataType"]);
                            dataTable.Columns.Add(column);
                        }
                        dataSet.Tables.Add(dataTable);
                        // Fill the data table we just created
                        while (reader.Read())
                        {
                            DataRow dataRow = dataTable.NewRow();
                            for (int i = 0; i < reader.FieldCount; i++)
                            {
                                dataRow[i] = reader.GetValue(i);
                            }
                            dataTable.Rows.Add(dataRow);
                        }
                    }
                    else
                    {
                        // No records were returned
                        DataColumn column = new DataColumn("RowsAffected");
                        dataTable.Columns.Add(column);
                        dataSet.Tables.Add(dataTable);
                        DataRow dataRow = dataTable.NewRow();
                        dataRow[0] = reader.RecordsAffected;
                        dataTable.Rows.Add(dataRow);
                    }
                }
                while (reader.NextResult());
                return dataSet;
            }
  • 相关阅读:
    C#中的编译为什么不叫Compile而叫build
    类型(Type)
    C#中关于值类型和引用类型的区别
    php通用化api格式输出
    tp6获取参数的五种办法
    PHP 数组------分割、合并
    explain结果中的type字段的含义
    order by 排序
    SQL开发技巧
    tp5模板输出日期时间
  • 原文地址:https://www.cnblogs.com/OleRookie/p/9529572.html
Copyright © 2020-2023  润新知