What is the best way to create an Empty DataTable object with the schema of a sql server table?
相关问题
- Sorting 3 numbers without branching [closed]
- sql execution latency when assign to a variable
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
Here's what I did:
Works well. Thanks AdaTheDev.
A statement I think is worth mentioning is SET FMTONLY:
The reason this can be handy is because you can supply any query/stored procedure and return just the metadata of the resultset.
Assuming that you can connect to the SQL database which contains the table you want to copy at the point it time you want to do this, you could use a conventional resultset to datatable conversion, using
as your source query.
This will return an empty result set with the structure of the source table.
All of these solutions are correct, but if you want a pure code solution that is streamlined for this scenario.
No Data is returned in this solution since CommandBehavior.SchemaOnly is specified on the ExecuteReader function(Command Behavior Documentation)
The CommandBehavior.SchemaOnly solution will add the SET FMTONLY ON; sql before the query is executed for you so, it keeps your code clean.