c# - SqlDataReader 为什么只返回第一行值?
问题描述
我需要返回列 Test 中的所有值,但不知何故只返回第一行值...
我试图添加 dr2.NextResult(); 到代码,但它没有帮助
SqlCommand command = conn.CreateCommand();
command.CommandText = "select Test from PrescTest ";
conn.Open();
command.ExecuteNonQuery();
SqlDataReader dr2 = command.ExecuteReader();
try
{
while (dr2.Read())
{
// get the results of column "Test"
string Tests = (string)dr2["Test"].ToString();
TextBox15.Text = Tests +" ";
}
conn.Close();
}
catch (SqlException sqlexception)
{
Response.Write("ERROR ::" + sqlexception.Message);
}
catch (Exception ex)
{
Response.Write("ERROR ::" + ex.Message);
}
finally
{
conn.Close();
}
解决方案
您被分配了值并删除了文本框的先前值。试试这个:
while (dr2.Read())
{
// get the results of column "Test"
string Tests = dr2["Test"].ToString();
TextBox15.Text += Tests +" ";
}
推荐阅读
- python - 尽管它是正确的,但 Spotify 发布请求会向我抛出“无效凭据”错误
- java - thymeleaf 和 spring boot 应用程序中未显示表单验证错误
- android - 当页面之一包含折叠工具栏并且 fitSystemWindows 为真时,Android BottomNavigationBar 移动
- r - 当 # of Obs 大于 26 时,二进制排列表变得太大
- python - 在 Moto 中使用 Boto3(1.8 或更高版本)时如何模拟 AWS 调用
- jquery - 如何使用当前 gif 制作预加载器背景以适应整个页面?
- r - 确定栅格堆栈中的哪个图层首先满足条件?
- mysql - 无法使用 awk 将数据插入 mysql 数据库
- scala.js - 升级到 1.0.0 版后不能再使用匿名类
- javascript - Wordpress“事件日历”编辑月视图