日期:2014-05-18 浏览次数:21507 次
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.Common; namespace Components { class DataReaderAdapter:DbDataAdapter { public int FillFromReader(DataTable dataTable, IDataReader dataReader) { return Fill(dataTable, dataReader); } protected override RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) { return null; } protected override RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) { return null; } protected override void OnRowUpdated(RowUpdatedEventArgs value) { } protected override void OnRowUpdating(RowUpdatingEventArgs value) { } } } #region 将DataReader 转为 DataTable /// <summary> /// 将DataReader 转为 DataTable /// </summary> /// <param name="DataReader">DataReader</param> public static DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader) { DataTable datatable = new DataTable(); try { DataTable schemaTable = dataReader.GetSchemaTable(); //int bun=schemaTable.Rows.Count; //DataColumn myDataColumn = new DataColumn(); //for(int bui=0;bui<bun;bui++) //{ //myDataColumn.DataType = schemaTable.Rows[0].GetType(); //myRow.GetType(); //myDataColumn.ColumnName = schemaTable.Rows[0].ToString(); //} //datatable.Columns.Add(myDataColumn); if (schemaTable != null) { foreach (DataRow myRow in schemaTable.Rows) { DataColumn myDataColumn = new DataColumn(); myDataColumn.DataType = myRow.GetType(); myDataColumn.ColumnName = myRow[0].ToString(); datatable.Columns.Add(myDataColumn); } } DataReader dra = new DataReader(); dra.FillFromReader(datatable, dataReader); dra = null; dataReader.Close(); } catch (Exception ex) { Error.Log(ex.ToString()); throw new Exception("转换出错!", ex); } return datatable; } #endregion