如何将 SQL 表数据读入 C# DataTable
我读过很多关于将 DataTable 插入 SQL 表的文章,但是如何将 SQL 表提取到 C#/.NET DataTable 中?
I've read a lot of posts about inserting a DataTable into a SQL table, but how can I pull a SQL table into a C#/.NET DataTable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在这里,试一下(这只是一个伪代码)
Here, give this a shot (this is just a pseudocode)
有很多方法。使用 ADO.NET 并在数据适配器上使用 fill 来获取 DataTable:
然后您可以从数据集中获取数据表。
在另一个答案中,不使用数据集。相反,它使用
Which is better to mine。
不过,我强烈建议您查看实体框架;使用数据表和数据集并不是一个好主意。它们没有类型安全性,这意味着只能在运行时进行调试。有了强类型集合(您可以通过使用 LINQ2SQL 或实体框架获得),您的生活将会变得更加轻松。
数据表 = 善,数据集 = 恶。如果您使用 ADO.NET,那么您可以使用这两种技术(EF、linq2sql、dapper、nhibernate、本月的 ORM),因为它们通常位于 ADO.NET 之上。您获得的优势是,只要您通过利用代码生成拥有正确的抽象级别,您就可以在模式更改时更轻松地更新模型。
ADO.NET 适配器使用公开数据库类型信息的提供程序,例如默认情况下它使用 SQL Server 提供程序,您也可以插入 - 例如 - devart PostgreSQL 提供程序,并且仍然可以访问类型信息,然后允许您如上所述使用您选择的 ORM(几乎无痛 - 有一些怪癖) - 我相信 Microsoft 也提供了一个 oracle 提供程序。这样做的全部目的是尽可能抽象数据库实现。
Lots of ways. Use ADO.NET and use fill on the data adapter to get a DataTable:
You can then get the data table out of the dataset.
In another answer, dataset isn't used. Instead, it uses
Which is preferable to mine.
I would strongly recommend looking at entity framework though; using datatables and datasets isn't a great idea. There is no type safety on them which means debugging can only be done at run time. With strongly typed collections (that you can get from using LINQ2SQL or entity framework) your life will be a lot easier.
Datatables = good, datasets = evil. If you are using ADO.NET then you can use both of these technologies (EF, linq2sql, dapper, nhibernate, ORM of the month) as they generally sit on top of ADO.NET. The advantage you get is that you can update your model far easier as your schema changes provided you have the right level of abstraction by levering code generation.
The ADO.NET adapter uses providers that expose the type info of the database, for instance by default it uses a SQL server provider, you can also plug in - for instance - devart PostgreSQL provider and still get access to the type info which will then allow you to as above use your ORM of choice (almost painlessly - there are a few quirks) - I believe Microsoft also provide an oracle provider. The ENTIRE purpose of this is to abstract away from the database implementation where possible.
独立于供应商的版本,完全依赖ADO.NET接口;两种方法:
我做了一些性能测试,第二种方法总是优于第一种方法。
Read1 看起来更好,但数据适配器的性能更好(不要混淆一个数据库优于另一个数据库,查询都是不同的)。两者之间的差异取决于查询。原因可能是
Load
需要逐行检查各种约束 来自文档添加行时(它是DataTable
上的一个方法),而Fill
位于 DataAdapter 上,DataAdapter 正是为此而设计的 - 快速创建 DataTable 。Vendor independent version, solely relies on ADO.NET interfaces; 2 ways:
I did some performance testing, and the second approach always outperformed the first.
Read1
looks better on eyes, but data adapter performs better (not to confuse that one db outperformed the other, the queries were all different). The difference between the two depended on query though. The reason could be thatLoad
requires various constraints to be checked row by row from the documentation when adding rows (its a method onDataTable
) whileFill
is on DataAdapters which were designed just for that - fast creation of DataTables.集中模型:您可以在任何地方使用它!
你只需要从你的函数调用下面的格式到这个类
就可以了。这是完美的方法。
Centerlized Model: You can use it from any where!
You just need to call Below Format From your function to this class
That's it. it's perfect method.
如果使用最新版本的C#(版本8之后),代码会变得更加简单,因为using语句不需要大括号。
If you use the latest version of C# (after version 8) , the code becomes even simpler, because the using statement does not need braces.