我在这里努力去理解(经过大量的研究之后)为什么我在C#中的MySQL查询只返回一行并且值是错误的。
public static void loadCombinedPaymentExportData(Int32 iFromId = 0)
{
// VERIFIED STATE OF CONNECTION (OUTPUTS "OPEN")
Console.Write(oConn.State+"\n");
// TRIED USING PREPARED STATEMENTS FIRST
//OdbcCommand oCommand = new OdbcCommand("SELECT * FROM v_payment_export_combined_rec WHERE id > ? LIMIT 10", oConn);
//oCommand.Parameters.Add("@ID", OdbcType.Int).Value = iFromId;
// ENDED UP TRYING A MORE DIRECT APPROACH SO I COULD SEE WHAT IS HAPPENING
string sQuery = "SELECT * FROM v_payment_export_combined_rec WHERE id > " + iFromId.ToString() + " LIMIT 10";
// OUTPUTS "SELECT * FROM v_payment_export_combined_rec WHERE id > 0 LIMIT 10"
Console.WriteLine(sQuery);
OdbcCommand oCommand = new OdbcCommand(sQuery, oConn);
using (OdbcDataReader oReader = oCommand.ExecuteReader())
{
// THIS ENTERS INTO THE BLOCK AS HAVING ROWS
if (oReader.HasRows)
{
while (oReader.Read())
{
// THIS ONLY OUTPUTS ONCE AS "Data: 1"
Console.WriteLine("\nData: {0}", oReader.GetString(0));
}
}
else
{
Console.WriteLine("No rows found.\n");
}
oReader.Close();
oCommand.Dispose();
}
}
这里的问题是如果我使用我的SQL编辑器在数据库上运行相同的确切查询,我会从视图中返回190k +行,而最低id
是8.事实上,我试过了添加额外的输出,如:
Console.WriteLine("\nData: {0} | {1} | {2}", oReader.GetString(0), oReader.GetString(1), oReader.GetString(2));
其中每一个都以 1 的形式返回,这是不正确的,因为第二列有真实文本。
奇怪的是,我之前使用相同的逻辑在另一张桌子上进行了类似的测试,它运行得很好。
我搞砸了什么?
[编辑]
尝试使用MySQL .NET Connector而不是ODBC,并获得相同的结果。
string sQuery = "SELECT * FROM v_payment_export_combined_rec WHERE id > " + iFromId.ToString() + " LIMIT 10;";
MySqlCommand oCommand = new MySqlCommand(sQuery, oConn);
using (MySqlDataReader oReader = oCommand.ExecuteReader())
{
if (oReader.HasRows)
{
while (oReader.Read())
{
Console.WriteLine("\nData: {0}", oReader.GetString(0));
}
}
else
{
Console.WriteLine("No rows found.\n");
}
oReader.Close();
oCommand.Dispose();
}
我会继续调查,看看能不知道发生了什么。