关于IDataReader.GetSchemaTable的一些事情

http://stackoverflow.com/questions/1574492/how-does-getschematable-work

The implementation of IDataReader.GetSchemaTable() is up to the provider - so it will vary. You can write your own providers and do it any way you want.

To be honest this is bad bit of design in the framework - you should never have interface methods that return an untyped DataTable or DataSet as that result could contain anything. Kinda defeats the point of constraining it by an interface in the first place: "you must have a method that returns DataTable but we don't care what rows or columns it has"

Even if the provider is SQL GetSchemaTable() doesn't go back to the [syscolumns] or [sysobjects]. That would be an additional DB call, require additional privileges and not work anyway, as the result set doesn't need to reflect any objects in the DB.

I'm not certain, but I'd expect the vast majority of IDataReader.GetSchemaTable() implementations to read some properties of the meta data held with the result set.

 

 

http://axislover.blog.163.com/blog/static/10776515200742792722670/

public static DataTable ConvertDataReaderToDataTable(SqlDataReader dataReader)
        {
            //把那个DataReader转化成DataTable。
            DataTable datatable = new DataTable();
            DataTable schemaTable = dataReader.GetSchemaTable();
            //动态添加列
            try
            {
                foreach (DataRow myRow in schemaTable.Rows)
                {
                    DataColumn myDataColumn = new DataColumn();
                    myDataColumn.DataType = myRow[0].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];
                        Type type = dataReader[i].GetType();
                        switch (type.Name)
                        {
                            case "String":
                                myDataRow[i] = (string)dataReader[i];
                                break;
                            case "Int16":
                                myDataRow[i] = (short)dataReader[i];
                                break;
                            case "Int32":
                                myDataRow[i] = (int)dataReader[i];
                                break;
                            case "Int64":
                                myDataRow[i] = (long)dataReader[i];
                                break;
                            case "DateTime":
                                myDataRow[i] = (DateTime)dataReader[i];
                                break;
                            case "Decimal":
                                myDataRow[i] = (decimal)dataReader[i];
                                break;
                            case "Char":
                                myDataRow[i] = (char)dataReader[i];
                                break;
                            case "Double":
                                myDataRow[i] = (double)dataReader[i];
                                break;
                            default:
                                myDataRow[i] = dataReader[i];
                                break;
                        }
                    }
                    datatable.Rows.Add(myDataRow);
                    myDataRow = null;
                }
                schemaTable = null;
                dataReader.Close();
                return datatable;
            }
            catch (Exception ex)
            {
                Error.Log(ex.ToString());
                throw new Exception("转换出错出错!", ex);
            }
        }

 

 

http://www.cnblogs.com/puresoul/archive/2010/06/29/1767333.html

http://weijingnawjn.blog.163.com/blog/static/3719707720091129101023132/

http://blog.csdn.net/jianxiong8814/article/details/2221635

posted on 2014-10-16 08:53  sPhinX  阅读(289)  评论(0编辑  收藏  举报

导航