VS2005下 DataReader 转为 DataTable 遇到困难 解决即揭贴
///定义DataTable和模式
DataTable datatable = new DataTable();
DataTable schemaTable = dataReader.GetSchemaTable();
try
{ ///动态添加表的数据列
foreach (DataRow myRow in schemaTable.Rows)
{
DataColumn myDataColumn = new DataColumn();
myDataColumn.DataType = myRow.GetType();
myDataColumn.ColumnName = myRow[0].ToString();
datatable.Columns.Add(myDataColumn);
}
///添加表的数据
while (dataReader.Read())
{
DataRow myDataRow = datatable.NewRow();
for (int i = 0; i < schemaTable.Rows.Count; i++)
{
myDataRow[i] = dataReader[i];
}
datatable.Rows.Add(myDataRow);
myDataRow = null;
}
schemaTable = null;
///关闭数据读取器
dataReader.Close();
return datatable;
}
catch (Exception ex)
{
///抛出类型转换错误
SystemError.SystemLog(ex.Message);
throw &nb