我在调用存储过程时用的ExecuteReader()方法,返回的数据是只读数据,怎样可以放到像dataset对象或是DataTable 中?谢谢啦!

解决方案 »

  1.   

    用DataAdapter来执行存储,相当于设置SelectCommand,然后用其的Fill来进行填充DataSet。
      

  2.   

    下一个SQLHELPER,直接调用里面的方法
      

  3.   

    string strSql="....";
    SqlConnection conn=New SqlConnection(.....);SqlDataAdapter adapter=new SqlDataAdapter(strSql,conn);
    DataSet ds=new DataSet();
    adapter.Fill(ds);
      

  4.   

    http://blog.csdn.net/xielingxu/archive/2007/01/20/1488417.aspx
      

  5.   

    楼上那些方法都对都行,问题是楼主说他想用Reader... 毕竟有人并不喜欢适配器.
    可以写个while循环,用Reader读到的内容填充DataTable的行.
      

  6.   

    #region 将DataReader 转为 DataTable
    /// <summary>
    /// 将DataReader 转为 DataTable
    /// </summary>
    /// <param name="DataReader">DataReader</param>
    public static DataTable ConvertDataReaderToDataTable(SqlDataReader reader)
    {
    try
    {                DataTable objDataTable = new DataTable();
                    int intFieldCount = reader.FieldCount;
                    for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
                    {
                        objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter));
                    }                objDataTable.BeginLoadData();                object[] objValues = new object[intFieldCount];
                    while (reader.Read())
                    {
                        reader.GetValues(objValues);
                        objDataTable.LoadDataRow(objValues, true);
                    }
                    reader.Close();
                    objDataTable.EndLoadData();                return objDataTable;            }
    catch(Exception ex)
    {
    throw new Exception("转换DataReader为DataTable出错!",ex);
    }

    }
    #endregion