How to load DataTable with reader and ignore schema? RRS feed

  • Question

  • Hi there,

    I want to load a datatable without any schema. Currently I do this:

      public DataTable LoadDataTable(string sqlText)
                DataTable table = new DataTable();
                using (SqlConnection conn = new SqlConnection(Configuracion.CadenaReporteador))
                    using (SqlCommand cmd = new SqlCommand(sqlText, conn))
                        using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                return table;
    However, the resulting DataTable includes schema information that I do not want (I want to insert arbitrary records).

    How can I do this?

    Thanks in advance.

    • Moved by VMazurModerator Monday, December 14, 2009 12:18 PM (From:ADO.NET Data Providers)
    Friday, December 11, 2009 10:10 PM


All replies

  • Hi Benton,

    After loading datatable use Copy Method I think copy method doesn't copy schema. Give it a try.

    01.private void CopyDataTable(DataTable table)
    03.    // Create an object variable for the copy.
    04.    DataTable copyDataTable = default(DataTable);
    06.        // 
    07.    copyDataTable = table.Copy();

    Friday, December 11, 2009 11:55 PM
  • Thanks for the suggestion. I found the Copy() solution to be a little heavy for my case, so I ended up loading the datatable with DataAdapter's Fill() method, which doesn't load the schema. Not what I wanted but oh well.

    BTW, as per MSDN, the Copy() method would not do what I wanted either:

    Return Value

    Type: System.Data.DataTable
    A new DataTable with the same structure (table schemas and constraints) and data as this DataTable.

    • Marked as answer by Benton Tuesday, December 15, 2009 3:24 PM
    Tuesday, December 15, 2009 3:20 PM