【发布时间】:2021-11-18 20:49:49
【问题描述】:
public string GetAllProducts()
{
string connStr = "server=localhost;user=root;database=tp1;port=3306;password=";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
Console.WriteLine("Connecting to MySQL...");
conn.Open();
string sql = "SELECT * FROM material";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
return (rdr[0] + " -- " + rdr[1] + " -- " + rdr[2] + " -- " + rdr[3]);
}
rdr.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Console.WriteLine("Done.");
return "No records found";
}
此时我在“材料”表上有四个条目。只返回第一个。
这是输出:
【问题讨论】:
标签: c# mysql-connector