zoukankan      html  css  js  c++  java
  • 关于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

  • 相关阅读:
    3.14周末作业
    3.13作业
    文件处理
    字符编码
    基本数据类型总结
    基本数据类型--------------------集合set()
    python入门009
    作业009
    python入门008
    作业008
  • 原文地址:https://www.cnblogs.com/s5689412/p/4027955.html
Copyright © 2011-2022 走看看