asp.net
using System;using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace OAManage.Components
{
/// <summary>
/// Tools 的摘要说明。
/// </summary>
public class Tools
{
#region 将DataReader 转为 DataTable
/// <summary>
/// 将DataReader 转为 DataTable
/// </summary>
/// <param name="DataReader">DataReader</param>
public static DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
{
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].ToString();
}
datatable.Rows.Add(myDataRow);
myDataRow = null;
}
schemaTable = null;
dataReader.Close();
return datatable;
}
catch(Exception ex)
{
throw new Exception("转换出错出错!",ex);
}
}
我们老师叫我们调试程序,粗体有错,好像是和数据库字段的数据类型不匹配,本人对.net编程不熟悉,我想在粗体部分进行数据类型判断,烦一大半天了还是没成功,难道要把数据库中int的字段都改成char吗?求大神支援,给点主意 asp.net --------------------编程问答-------------------- http://blog.csdn.net/a3676212/article/details/3863639 --------------------编程问答--------------------
补充:.NET技术 , ASP.NET