/// <summary> /// SqlDataReader 轉成 DataTable /// 源須要是結果集 /// </summary> /// <param name="dataReader"></param> /// <returns></returns> private 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); } }