Asp.net SqlDataReader转成Datatable
内容摘要:Asp.net 数据存储,呈现方式有多种,如DataSet,DataTable,DataView,DataReader,IList等等,做得多了,常会需要将数据存储类型相互转换,这边介绍DataReader转DataTable的方法。
- /// <summary>
- /// SqlDataReader转成DataTable实现方法
- /// </summary>
- /// <param name="strSql"></param>
- /// <returns></returns>
- protected DataTable DataReaderToDataTable(string strSql)
- {
- string connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
- SqlConnection sqlConn = new SqlConnection(connstr);
- SqlCommand sqlComm = new SqlCommand(strSql, sqlConn);
- SqlDataReader dataReader = null;
- DataTable dtReturn = null;
- object[] value = null;
- try
- {
- sqlComm.Connection.Open();
- dataReader = sqlComm.ExecuteReader();
- if (dataReader.HasRows)
- {
- dtReturn = CreateTableBySchemaTable(dataReader.GetSchemaTable());
- value = new object[dataReader.FieldCount];
- while (dataReader.Read())
- {
- dataReader.GetValues(value);
- dtReturn.LoadDataRow(value, true);
- }
- value = null;
- }
- }
- catch (Exception err)
- {}
- finally
- {
- dataReader.Close();
- sqlComm.Connection.Close();
- sqlComm.Dispose();
- }
- return dtReturn;
- }
- /// <summary>
/// 本方法创建表各个列的名
/// </summary>
/// <param name="pSchemaTable">是一个表的结构表</param>
/// <returns></returns> - protected DataTable CreateTableBySchemaTable(DataTable pSchemaTable)
- {
- DataTable dtReturn = new DataTable();
- DataColumn dc = null;
- DataRow dr = null;
- for (int i = 0; i < pSchemaTable.Rows.Count; i++)
- {
- dr = pSchemaTable.Rows[i];
- dc = new DataColumn(dr["ColumnName"].ToString(), dr["DataType"] as Type);
- dtReturn.Columns.Add(dc);
- }
- dr = null;
- dc = null;
- return dtReturn;
- }
/// <summary> /// SqlDataReader转成DataTable实现方法 /// </summary> /// <param name="strSql"></param> /// <returns></returns> protected DataTable DataReaderToDataTable(string strSql) { string connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection sqlConn = new SqlConnection(connstr); SqlCommand sqlComm = new SqlCommand(strSql, sqlConn); SqlDataReader dataReader = null; DataTable dtReturn = null; object[] value = null; try { sqlComm.Connection.Open(); dataReader = sqlComm.ExecuteReader(); if (dataReader.HasRows) { dtReturn = CreateTableBySchemaTable(dataReader.GetSchemaTable()); value = new object[dataReader.FieldCount]; while (dataReader.Read()) { dataReader.GetValues(value); dtReturn.LoadDataRow(value, true); } value = null; } } catch (Exception err) {} finally { dataReader.Close(); sqlComm.Connection.Close(); sqlComm.Dispose(); } return dtReturn; }
/// <summary>
/// 本方法创建表各个列的名
/// </summary>
/// <param name="pSchemaTable">是一个表的结构表</param>
/// <returns></returns> protected DataTable CreateTableBySchemaTable(DataTable pSchemaTable) { DataTable dtReturn = new DataTable(); DataColumn dc = null; DataRow dr = null; for (int i = 0; i < pSchemaTable.Rows.Count; i++) { dr = pSchemaTable.Rows[i]; dc = new DataColumn(dr["ColumnName"].ToString(), dr["DataType"] as Type); dtReturn.Columns.Add(dc); } dr = null; dc = null; return dtReturn; }
方法来自互联网,忘记从哪来的,在此注明。
作者:www.btnan.com,来源于:http://www.btnan.com,尊重作者,转载请注明。