需要在.net中使用数据库表模式获取空数据表
使用 sql server 表的架构创建空 DataTable 对象的最佳方法是什么?
What is the best way to create an Empty DataTable object with the schema of a sql server table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
所有这些解决方案都是正确的,但如果您想要一个针对此场景简化的纯代码解决方案。
由于在 ExecuteReader 函数上指定了 CommandBehavior.SchemaOnly,因此此解决方案中未返回任何数据(命令行为文档)
CommandBehavior.SchemaOnly 解决方案将添加 SET FMTONLY ON; sql 在为您执行查询之前,这样可以保持代码干净。
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.
尝试:
SELECT TOP 0 * FROM [TableName]
并使用 SQLDataAdapter 填充数据集,然后从该数据集中获取表。
Try:
SELECT TOP 0 * FROM [TableName]
and use SQLDataAdapter to fill a DataSet, then get the Table from that DataSet.
我认为值得一提的声明是 SET FMTONLY:
这很方便的原因是您可以提供任何查询/存储过程并仅返回结果集的元数据。
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.
假设您可以连接到包含要在您想要执行此操作时复制的表的 SQL 数据库,则可以使用传统结果集到数据表的转换,用作
源查询。
这将返回一个具有源表结构的空结果集。
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.
这就是我所做的:
效果很好。谢谢阿达德夫。
Here's what I did:
Works well. Thanks AdaTheDev.
这有效:
this works:
我知道这是一个老问题并且特定于 SQL Server。但是,如果您正在寻找可跨不同数据库工作的通用解决方案,请使用 Richard 的解决方案,但将其修改为使用
"SELECT * FROM {0} WHERE 1=0"
并将类型更改为使用通用ADO.Net 类型 IDataReader、IDbCommand 等。大多数现代关系数据库都足够智能,可以识别 1=0 条件,并且不会像常规表扫描查询那样运行它。我已经在 SQL Server、Oracle 和 DB2 上尝试过了,表也只有很少的 1 亿条记录。所有这些都会在几毫秒内返回空结果。
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.
这就是我所做的,它提供了一个可供使用的空白数据表:
Here's what I did, which provides a blank DataTable ready to be used:
您始终可以创建自己的:
明显的缺点是每当数据库架构发生更改时您都必须更新代码。
You can always create your own:
The obvious draw back being that you will have to update your code whenever the database schema changes.