Dej*_*ric 4 sql-server ado.net select
我想从表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();
Run Code Online (Sandbox Code Playgroud)
为什么这总是只返回第一行.实际上它返回一行,因为如果我在where子句中设置类似的where id = 2 and id = 3
东西仍然只返回一个值.从Management Studio检查表有多个值,查询运行应该如此.
提前致谢.
因为您没有遍历查询结果,所以它只显示一个结果.
string query = "SELECT * FROM Account";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(query, connection);
SqlDataReader reader;
connection.Open();
reader = command.ExecuteReader();
While(reader.Read())
{
label1.Text += " " +reader["PasswordHash"].ToString();
}
connection.Close();
Run Code Online (Sandbox Code Playgroud)
上面的代码循环遍历查询结果,并在分配给的连接字符串中为您提供所需的内容label1.text
.您还可以通过将查看结果Console.WriteLine(reader["PasswordHash"].ToString());
的while
循环