【发布时间】:2012-09-21 12:51:19
【问题描述】:
我想从表 Account 中选择多个(所有)值。
string query = "SELECT * FROM Account";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader;
connection.Open();
reader = command.ExecuteReader();
reader.Read();
label1.Text = reader["PasswordHash"].ToString();
connection.Close();
为什么总是只返回第一行。实际上它返回一行,因为如果我在 where 子句中设置类似where id = 2 and id = 3 它仍然只返回一个值。
表有多个值,我在 Management Studio 中检查过,查询运行正常。
提前致谢。
【问题讨论】:
-
我很好奇:如果您想从结果集中读取多行,您希望 label1.Text 完成后的样子?
-
因为你只给
reader.Read()打了一次电话?!?!?!?该调用读取结果集的 一行 - 仅此而已。 -
这只是练习以完全掌握 sql server 功能。标签是最容易开始的事情。
标签: sql-server select ado.net