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
this works:
I know that this is an old question and specific to SQL Server. But if you are looking for generic solution that will work across different databases, use Richard's solution, but modify it to use
"SELECT * FROM {0} WHERE 1=0"
and change the types to use generic ADO.Net types IDataReader, IDbCommand etc.Most modern relational databases are intelligent enough to identify the 1=0 condition and will not run it like a regular tablescan query. I have tried this on SQL Server, Oracle and DB2 with tables have few 100 million records also. All do return empty result back in matter of few milliseconds.
Try:
SELECT TOP 0 * FROM [TableName]
and use SQLDataAdapter to fill a DataSet, then get the Table from that DataSet.
You can always create your own:
The obvious draw back being that you will have to update your code whenever the database schema changes.
Here's what I did, which provides a blank DataTable ready to be used: