从SqlDataReader读取数据
我有一个 SQL Server 2008 数据库,我正在后端处理它。我正在研究 ASP.NET/C#
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
//how do I read strings here????
}
我知道读者有价值观。我的 SQL 命令是从表中仅选择 1 列。该列仅包含字符串。我想一一读取阅读器中的字符串(行)。我该怎么做?
I have a SQL Server 2008 database and I am working on it in the backend. I am working on ASP.NET/C#
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
//how do I read strings here????
}
I know that the reader has values. My SQL command is to select just 1 column from a table. The column contains strings ONLY. I want to read the strings (rows) in the reader one by one. How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我知道这有点旧,但如果您将 SqlDataReader 的内容读入类中,那么这将非常方便。 reader 和 class 的列名应该相同
I know this is kind of old but if you are reading the contents of a SqlDataReader into a class, then this will be very handy. the column names of reader and class should be same
我会反对在这里使用
SqlDataReader
; ADO.NET 有很多的边缘情况和复杂性,根据我的经验,大多数手动编写的 ADO.NET 代码至少以一种方式(通常是微妙的和上下文的)被破坏。存在避免这种情况的工具。例如,在这里的情况下,您想要读取一列字符串。 Dapper 使这一切变得完全轻松:
这里的 Dapper 负责处理所有参数化、执行和行处理- 以及 ADO.NET 的许多其他蹩脚细节。
可以替换为
以将整行具体化为对象。I would argue against using
SqlDataReader
here; ADO.NET has lots of edge cases and complications, and in my experience most manually written ADO.NET code is broken in at least one way (usually subtle and contextual).Tools exist to avoid this. For example, in the case here you want to read a column of strings. Dapper makes that completely painless:
Dapper here is dealing with all the parameterization, execution, and row processing - and a lot of other grungy details of ADO.NET. The
<string>
can be replaced with<SomeType>
to materialize entire rows into objects.您必须在此处
阅读
数据库列
。您可以查看以下代码片段以进行实现:You have to
read
database column
here. You could have a look on following code snippet for implementation:事实上,我自己发现我可以这样做:
Actually, I figured it out myself that I could do this:
用最简单的术语来说,如果您的查询返回 column_name 并且它包含一个字符串:
In the simplest terms, if your query returns column_name and it holds a string:
我通常通过数据读取器这样读取数据。只是添加了一个小例子。
I usually read data by data reader this way. just added a small example.
我有一个辅助函数,例如:
然后我用它来提取字符串:
I have a helper function like:
then I use it to extract the string:
或
这些是
对象
,因此您需要强制转换它们或.ToString()
。or
These are
object
s, so you need to either cast them or.ToString()
.将从数据库返回的列的名称放在
"ColumnName"
所在的位置。如果是字符串,可以使用.ToString()
。如果是其他类型,则需要使用System.Convert
进行转换。Put the name of the column begin returned from the database where
"ColumnName"
is. If it is a string, you can use.ToString()
. If it is another type, you need to convert it usingSystem.Convert
.会起作用的
it wil work
想与那些可以使用它的人分享我的辅助方法:
用法:
辅助方法可以转换为您喜欢的任何值,如果无法转换或数据库值为 NULL,则结果将为 null。
Thought to share my helper method for those who can use it:
Usage:
The helper method casts to any value you like, if it can't cast or the database value is NULL, the result will be null.
对于单个结果:
对于多个结果:
For a single result:
For multiple results: