在 C# 中执行存储过程的最快方法需要最少的代码
我将获取实体集合中的结果。我不仅仅需要 IDataReader 对象。因此,考虑到我没有大量参数并且 SP 返回了许多数据列,因此应该回答上面的问题。
请说明理由。
有些可能是
拖放到 DBML 设计器上,然后通过 linq to sql 调用它。那很好。但这也会生成大量 DBML 代码。越简单越好。
通过执行
ExecuteReader
获取IDataReader,然后将IDataReader传递给工厂以创建它的对象。又好又简单。但需要大量编码。返回的每一列都需要以下内容。
int nameIndex = dataReader.GetOrdinal("名称"); if (!dataReader.IsDBNull(nameIndex)){ myObject.Name = dataReader.GetString(nameIndex); }
还有其他更简单的选择吗?
更新
@Heinzi 的答案简化了处理结果的过程。有没有更好的方法来处理大量参数?
I'm going to grab the results in a collection of entity. I don't just need the IDataReader object. So above should be answered considering that I've large no of parameters and many columns of data is getting returned by the SP.
Please provide reasons.
A few might be
Drag and drop on to a DBML designer and then calling it by linq to sql. That's very good. But that also generates lot of code for the DBML. Simpler is better.
Getting IDataReader by doing
ExecuteReader
and then passing IDataReader to factory to create it's object. Good and simple. But requires lot of coding. Below is required for each column returned.
int nameIndex = dataReader.GetOrdinal("Name"); if (!dataReader.IsDBNull(nameIndex)){ myObject.Name = dataReader.GetString(nameIndex); }
Are there any other simpler option for this?
UPDATE
@Heinzi's answer simplifies the process of handling the results. Is there any better way to handle large number of parameters also?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想坚持使用 DataReaders,您可以通过在 IDataReader 上编写扩展方法来节省一些代码,即类似这样的内容(未经测试):
这会将您的工厂代码减少为
当然,此扩展方法的无数变体是可能的(与或者没有 defaultValue,目标字段作为 ref 参数传递以获得与代码完全相同的行为等)。
否则,您可以使用 DataSets 而不是 DataReaders,其中已经存在这样的扩展方法。您可以像这样创建数据集(而不是执行
myCommand.ExecuteReader
):然后您可以使用 DataRowExtensions.Field:
If you want to stick to DataReaders, you can save some code by writing an extension method on IDataReader, i.e., something like this (untested):
This would reduce your factory code to
Of course, countless variants of this extension method are possible (with or without defaultValue, target field passed as a ref parameter to get exactly the same behavior as your code, etc.).
Otherwise, you could use DataSets instead of DataReaders, where an extension method like this already exists. You create your DataSet like this (instead of executing
myCommand.ExecuteReader
):Then you can access your data using DataRowExtensions.Field: