c# - 从 SqlDataReader 读取数据

标签 c# asp.net sql-server-2008 sqldatareader

我有一个 SQL Server 2008 数据库,我正在后端处理它。我正在开发 asp.net/C#

SqlDataReader rdr = cmd.ExecuteReader();  
while (rdr.Read())  
{              
   //how do I read strings here????  
}  

我知道读者有值(value)观。我的 SQL 命令是从表中只选择 1 列。该列仅包含字符串。我想一个一个地读取阅读器中的字符串(行)。我该怎么做?

最佳答案

using(SqlDataReader rdr = cmd.ExecuteReader())
{
    while (rdr.Read())
    {
        var myString = rdr.GetString(0); //The 0 stands for "the 0'th column", so the first column of the result.
        // Do somthing with this rows string, for example to put them in to a list
        listDeclaredElsewhere.Add(myString);
    }
}

关于c# - 从 SqlDataReader 读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4018114/

相关文章:

c# - WCF 错误 "The socket connection has been disposed"

c# - 任何优秀的 C# 开源网络爬虫框架

C#:当 Reader 只有一个元素时,我应该使用 "if"而不是 "while"吗?

c# - 如何在图表中绘制日期和时间?

c# - 从网页向数据库插入数据时,是否每次都使用 Trim 函数?

windows - 如何说服客户升级到 WCF 或为 Remoting 和 WCF 保留两个代码库

SQL Pivot on 文本列返回多行

SQL:如何根据同一表中其他列的值创建新列

c# - unity3d 中的陀螺仪有什么问题(在 google nexus 7 上测试过)

asp.net - 自动调整文本区域大小(多行 asp :TextBox)