Need to get empty datatable in .net with database table schema

前端 未结 10 806
闹比i
闹比i 2021-02-07 04:40

What is the best way to create an Empty DataTable object with the schema of a sql server table?

10条回答
  •  隐瞒了意图╮
    2021-02-07 05:14

    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.

提交回复
热议问题