如何使用 .NET 2.0 从 SQL Server 中选择数据——尽可能简单
简单的问题:
我有一个应用程序需要向 SQL Server 2005 发出六个 SELECT 请求并将结果写入平面文件。就是这样。
如果我可以使用 .NET 3.5,我会创建一个 LINQ-To-SQL 模型,编写 LINQ 表达式并在一小时内完成。鉴于我无法使用 .NET 3.0 或 3.5,下一个最佳方法是什么? ADO.NET DataReaders/DataSets 是最佳选择,还是我忘记了其他可用的东西?
Easy question:
I have an app that needs to make a half dozen SELECT requests to SQL Server 2005 and write the results to a flat file. That's it.
If I could use .NET 3.5, I'd create a LINQ-To-SQL model, write the LINQ expressions and be done in an hour. What is the next best approach given that I can't use .NET 3.0 or 3.5? Are ADO.NET DataReaders/DataSets the best option, or am I forgetting something else available?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 SqlCommand 和 SqlDataReader 类是最好的选择。如果需要将结果写入平面文件,则应直接使用读取器而不是转到 DataSet,因为后者会将结果加载到内存中,然后才能将其写入平面文件。
SqlDataReader 允许您以流式传输方式读出数据,使您的应用程序在这种情况下更具可扩展性。
Using the SqlCommand and SqlDataReader classes are your best bet. If you need to write the results to a flat file, you should use the reader directly instead of going to a DataSet, since the latter will load the result in memory before you're able to write it out to a flat file.
The SqlDataReader allows you to read out the data in a streaming fashion, making your app a lot more scalable for this situation.
正如 Nick K 对我的 SQL Server 2000 的回答非常有帮助关于服务器故障的问题,bcp 实用程序对此非常方便。
您可以编写一个批处理文件或快速脚本,通过查询调用 BCP,并将 csv、sql 直接转储到文本文件!
As Nick K so helpfully answered on my SQL Server 2000 question on serverfault, the bcp utility is really handy for this.
You can write a batch file or quick script that call BCP with your queries and have it dump csv,sql direct to a text file!
同意 Dave Van den Eynde 上面的回答,但我想说,如果您将大量数据推送到这些文件中,并且您的应用程序可以支持它,那么值得考虑制作一个 SSIS 包。
对此可能完全是矫枉过正,但批量导入/导出时经常忽视这一点。
Agree with Dave Van den Eynde's answer above, but I would say that if you're pushing a large amount of data into these files, and if your app is something that can support it, then it's worth taking a look at making an SSIS package.
Could be complete overkill for this, but it's something that is often overlooked for bulk import/export.
或者,您可以避免编写代码并使用 BCP.exe:
http://msdn.microsoft.com/en-us /library/ms162802(SQL.90).aspx
Alternatively, you could avoid writing code and use BCP.exe:
http://msdn.microsoft.com/en-us/library/ms162802(SQL.90).aspx