Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 选择*仅返回一个值_Sql Server_Select_Ado.net - Fatal编程技术网

Sql server 选择*仅返回一个值

Sql server 选择*仅返回一个值,sql-server,select,ado.net,Sql Server,Select,Ado.net,我想从表Account中选择多个(全部)值 string query = "SELECT * FROM Account"; SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(query, connection); SqlDataReader reader; connection.Ope

我想从表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子句中设置类似于
whereid=2和id=3的内容,它仍然只返回一个值。
表中有多个我在ManagementStudio中检查过的值,那个里的查询按它们应该的方式运行


提前感谢。

因为您没有循环查询结果,所以它只显示一个结果

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();

上面的代码循环查询结果,并将在指定给
label1.text
的串联字符串中提供所需内容。您还可以通过插入
Console.WriteLine(reader[“PasswordHash”].ToString())来查看结果
循环中,而
循环

读卡器一次只前进一条记录。您需要在循环中迭代结果集:

while (reader.Read()) {
    // do something with the current row by accessing reader[]
}
reader.Close();
有更好的方法来组织代码,但这说明了您缺少的一点,并且需要最少的更改。

reader.Read()
将获得下一行,因此您可以使用
while(reader.Read())
来迭代行。

您应该这样做

while (reader.Read())
    // process information
您需要迭代检索到的所有信息


旁注:在SqlConnection、SqlCommand和SqlDataReader上使用using语句,以确保正确处理对象。

查看您的代码,我的建议是使用其他对象来添加代码。标签文本不是合适的选择

尝试在列表中使用foreach循环来检索已返回的所有数据。

您需要一个while循环

while(reader.Read()) {
           Console.WriteLine("{0}", reader[0]);
        }

使用
其中id=2和id=3将返回零结果,因为id=2和id=3是互斥的<代码>其中id=2或id=3可以工作

while (reader.Read()) { /*Do stuff with current row*/ }

可以用于迭代结果

您需要循环,如下所示:

SqlDataReader reader = command.ExecuteReader();

// Call Read before accessing data.
while (reader.Read())
{
    Console.WriteLine(String.Format("{0}, {1}",
        reader[0], reader[1]));
}
SqlDataReader myReader = myCommand.ExecuteReader();
// Always call Read before accessing data.
while (myReader.Read()) {
   Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}
// always call Close when done reading.
myReader.Close();
MSDN文档中有一个示例:


您需要循环,数据读取器是代码的最佳方式,对于示例,如下所示:

SqlDataReader reader = command.ExecuteReader();

// Call Read before accessing data.
while (reader.Read())
{
    Console.WriteLine(String.Format("{0}, {1}",
        reader[0], reader[1]));
}
SqlDataReader myReader = myCommand.ExecuteReader();
// Always call Read before accessing data.
while (myReader.Read()) {
   Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
}
// always call Close when done reading.
myReader.Close();

我很好奇:如果您想从结果集中读取多行,那么您希望label1.Text在完成后是什么样子?因为您只调用了
reader.read()
一次?!?!?!?该调用只读取结果集的一行-仅此而已。这只是练习完全掌握sql server功能。标签是最容易开始的。我在想“或”,但我写了“和”: