【发布时间】:2014-02-18 03:29:46
【问题描述】:
我正在尝试在 Visual C# 控制台应用程序上打印 MySQL 查询的结果。正如您在下面看到的那样,我能够在一行上获得多个列,但我想知道如何获得多个结果(行)。你看,我的表里有更多符合查询条件的记录。有人可以帮帮我吗?
class Program
{
static void Main(string[] args)
{
string ConnectionString = "Server=localhost; Database=world; Uid=root; Pwd=password"; // giving connection string
MySqlConnection connection = new MySqlConnection(ConnectionString);
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT name, population FROM city where population > 4000000";
try
{
connection.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("City name is: " + reader["name"].ToString() + " " + reader["population"].ToString());
Console.Read();
}
}
【问题讨论】:
-
在 while 循环中真的需要 Console.Read() 吗?你知道这个等待用户输入的块吗?这可能是您只看到一个结果的原因吗?
-
您说得对,您能正式回复一下,以便我接受您的问题吗?它现在正在工作。谢谢。
标签: c# mysql visual-studio