我试图在 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();
}
}
最佳答案
您对 Console.Read() 的调用阻塞了 while 循环,因此您只向控制台打印一行,然后等待用户输入。
干杯
关于c# - 在 Visual C# 控制台应用程序上获取多个 MySQL 查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21843768/