需要更正或更好的方法来通过 SQL ADO.NET 读取数据
目前,我使用 DataSet 从 ADO.NET SQL 提供程序读取数据,但我了解到 DataReader 在某些情况下效率更高。很多时候我只读取单个/几条记录,不需要内存数据或多个表关系操作。
是否有更干净、高效的阅读方式?
我可以以简单的方式将完整记录直接映射到 Agent 类,而不必像现在一样解析每一列吗?
编辑: - 我对 ORM 进行了一些调查,并使用了一点 Linq2SQL,但熟悉度曲线似乎有点陡峭,所以就这样吧。请建议 .NET 中可用的其他方法,而不是任何外部工具。
public override Agent GetAgentByAgentId(string agentId)
{
Agent agent;
try
{
ArrayList paramList = new ArrayList();
paramList.Add(_dbUtilities.CreateSqlParamater("@agent_id", SqlDbType.VarChar, 10, ParameterDirection.Input, agentId));
// Query the database for an agent with given agentId
DataSet ds = _dbLib.ExecuteProcedureDS("sp_dbc_agentsSelect", paramList);
if (ds != null && ds.Tables != null && ds.Tables.Count > 0)
{
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
agent = new Agent();
DataRow dr = dt.Rows[0];
// Get the agent data
agent.IsActive = bool.Parse(dr["is_active"].ToString());
agent.UserId = dr["user_id"].ToString();
....
}
}
currently I use DataSet
to read data from ADO.NET SQL provider, but I read that DataReader
is more efficient for some cases. Many a times I only read single/few records and don't need in-memory data or multiple table relation manipulations.
Are there more clean and efficient ways to read ?
Can I in simple way map the full record directly to the Agent class, without having to parse out each column like I do now?
EDIT: - I have investigated somewhat on ORMs and used Linq2SQL a little, but the familiarity curve seems little steep, so left it at that. Please suggest other ways available within .NET and not any external tools.
public override Agent GetAgentByAgentId(string agentId)
{
Agent agent;
try
{
ArrayList paramList = new ArrayList();
paramList.Add(_dbUtilities.CreateSqlParamater("@agent_id", SqlDbType.VarChar, 10, ParameterDirection.Input, agentId));
// Query the database for an agent with given agentId
DataSet ds = _dbLib.ExecuteProcedureDS("sp_dbc_agentsSelect", paramList);
if (ds != null && ds.Tables != null && ds.Tables.Count > 0)
{
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
agent = new Agent();
DataRow dr = dt.Rows[0];
// Get the agent data
agent.IsActive = bool.Parse(dr["is_active"].ToString());
agent.UserId = dr["user_id"].ToString();
....
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,我想推荐 SLAk 的答案。这确实是你问题的答案。我理解您对使用 EF 等大型工具集的担忧,但这确实是正确的解决方案,并且没有您想象的那么多学习曲线。几乎没有理由再直接使用 DataReader 之类的东西了。 EF 是 .NET 的一部分,我极力鼓励您使用它。
同样,不要走上创建自己的带有装饰和自动代码生成之类的 ORM 的道路。做。不是。做。它。最后,您将花费与维护业务逻辑一样多的时间,并且您会因为没有使用许多比我聪明得多的人的工作而自责,可能也比您聪明;) (当然,无意冒犯)。如果你正在寻找快速而肮脏的方法,那么你就需要快速而肮脏的方法。
话虽这么说,使用 DataReader 是从数据库读取数据的最轻量级方法。
除了给您提供一个在 C# 中使用
DataReader
的示例之外,我不确定如何准确回答您的第一个问题。while 语句将对从读取器检索到的每一行执行,尽管您当然可以根据需要短路该循环而不解析整个结果集。
First, I'd like to recommand SLak's answer. This really is the answer to your question. I understand your trepidation about using large toolsets like EF, but it's really the right solution and doesn't have as much of a learning curve as you might think. There's very little reason to go straight to things like a
DataReader
anymore. EF is part of .NET and I can't encourage you enough to use it.Likewise, don't go down the road of creating your own ORM of sorts with decorations and automatic code generation and the like. Do. Not. Do. It. In the end, you'll spend as much time maintaining that as you do your business logic, and you'll kick yourself for not using the work of many people who are a lot smarter than I am and probably you, as well ;) (No offense, of course). If you're looking for quick-and-dirty, then you need to go quick-and-dirty.
That being said, using a
DataReader
is the most lightweight method for reading data from the database.I'm not sure exactly how to answer your first question, other than to give you an example of using a
DataReader
in C#.The
while
statement will execute for every row retrieved from the reader, though you can certainly short-circuit that loop if need be and not parse the entire result set.您可以使用 ORM,例如 Microsoft Entity Framework。
它们将自动生成 SQL 并将属性复制到业务对象。
You can use an ORM, such as Microsoft Entity Framework.
They will automatically generate SQL and copy properties to business objects.
从 ORM 回到直接的 SQL 和 ADO 激励我创建一种简单的方法来进行数据提取,而不是像您现在那样(以及我以前的方式)。
这里有很多代码要显示,但我将简要概述如何处理这个问题。
创建一个继承自
Attribute
的Column
类在您的
Agent
类上像这样装饰您的 Properties:从现在起您应该能够获得这些自定义属性类 Properties 的属性并获取一列并设置属性值:
如果你做对了,你最终应该得到类似的结果:
该方法的签名可能是:
Going back to straight SQL and ADO from an ORM inspired me to create a simple way to do data extraction rather than the way you're doing it (and the way I used to do it).
There is a lot of code to show here but I will give you a brief overview of how you can take care of this.
Create a
Column
class that inherits fromAttribute
On your
Agent
class decorate your Properties like so:From this point you should be able to get those custom attributes off of the class Properties and get a column and set a property value:
If you get this right, you should end up with something like:
where the signature of that method could be:
我目前使用 n 层环境进行开发。我有一个核心数据类,我的所有数据层类都继承自该核心数据类,它是单个数据库连接。它有执行我的存储过程或 t-sql 的方法。这些方法可以返回我的业务对象用来加载变量的 sqlreader 对象。我将发布一些示例。我希望这有帮助。
这是我的核心数据类:
这是一个简单的数据层类:
这是一个使用此数据层类的业务层类。需要注意的部分是加载和保存方法。主要是负载。
I currently use an n-tier environment for my development. I have a core data class that all of my data layer classes inherit from that is a single database connection. It has methods to execute my stored procs or t-sql. These methods can return sqlreader objects that my business objects use to load the variables. I'll post some examples. I hope this helps.
Here is my core data class:
Here is a simple data layer class:
Here is a business layer class that consumes this data layer class. The parts to pay attention to are the load and save methods. Mostly the load.