gpt4 book ai didi

c# - MySqlDataReader 没有读取我的查询,就好像没有返回行一样,但 PHPMyAdmin 返回具有相同查询的行

转载 作者:行者123 更新时间:2023-11-30 22:14:59 25 4
gpt4 key购买 nike

第一次在这里请求您的帮助。

我知道我的标题是错误的,如果我们弄清楚是什么问题,我会更改它,此刻我有点迷茫。

好吧,我们先开始,我想从我的数据库中选择当前用户范围内的所有用户。

如果我执行查询,它会从 PHPMyAdmin 返回 4 行,哦,我应该提到我正在使用 MariaDB

这里是查询:

SET @orig_lat=45.820904;
SET @orig_lon=-73.760549;
SET @dist=1000 * 5;//5 KM
SET @accountId=9;

SELECT SQL_CALC_FOUND_ROWS *, (((acos(sin((@orig_lat*pi()/180)) * sin((account.latitude*pi()/180))+cos((@orig_lat*pi()/180))*cos((account.latitude*pi()/180))*cos(((@orig_lon-account.longitude)*pi()/180))))*180/pi())*60*1.1515*1609.344) AS distance FROM accounts AS account HAVING distance <= @dist AND account.id <> @accountId ORDER BY distance ASC LIMIT 50 OFFSET 0;0

现在从 C# 看它是这样的:

public static Dictionary<List<AccountData>, int> SearchAccounts(string accountId, double latitude, double longitude, int rangeInKM, int limit, int page = 1){
List<AccountData> returnList = new List<AccountData>();
int total_count = 0;
query = "SELECT SQL_CALC_FOUND_ROWS *, (((acos(sin((@orig_lat*pi()/180)) * sin((account.latitude*pi()/180))+cos((@orig_lat*pi()/180))*cos((account.latitude*pi()/180))*cos(((@orig_lon-account.longitude)*pi()/180))))*180/pi())*60*1.1515*1609.344) AS distance FROM accounts AS account HAVING distance <= @dist AND account.id <> @accountId ORDER BY distance ASC LIMIT @limit OFFSET @page;";
var rows = Database.ExecuteQuery(query,
new QueryParameter("accountId", accountId),
new QueryParameter("orig_lat", latitude),
new QueryParameter("orig_lon", latitude),
new QueryParameter("dist", 1000 * rangeInKM),
new QueryParameter("limit", limit),
new QueryParameter("page", page - 1));

ExecuteQuery函数代码:

public override DatabaseRow[] ExecuteQuery (string query, params QueryParameter[] parameters)
{
try
{
using (MySqlConnection connection = new MySqlConnection (connectionString))
{
using (MySqlCommand command = new MySqlCommand (query, connection))
{
foreach (QueryParameter parameter in parameters)
command.Parameters.AddWithValue(parameter.name, parameter.obj);

connection.Open();
using (MySqlDataReader reader = command.ExecuteReader())
{
int fieldCount = reader.FieldCount;
List<DatabaseRow> rows = new List<DatabaseRow> ();

while (reader.Read())
{
//For each row create a DatabaseRow
DatabaseRow row = new DatabaseRow();

//And add each field to it
for (int i = 0; i < fieldCount; i++)
{
row.Add(
reader.GetName(i),
reader.GetValue(i)
);
}

//Add it to the rows
rows.Add(row);
}

return rows.ToArray();
}
}
}
}
catch(MySqlException e)
{
throw new DatabaseException(e.Message, e);
}
}

请注意循环 while(reader.read()) ... 永远不会“触发”,就好像它们没有匹配查询的行一样。

现在我有点迷失了我应该做些什么来解决这个问题,欢迎所有帮助!

非常感谢您的帮助:)

最佳答案

好吧,我想出了我的问题所在,我在查询参数中将纬度解析为经度。

我留下它以防有人需要代码。始终仔细检查您的参数;)

需要改变

var rows = Database.ExecuteQuery(query,
new QueryParameter("accountId", accountId),
new QueryParameter("orig_lat", latitude),
new QueryParameter("orig_lon", latitude),
new QueryParameter("dist", 1000 * rangeInKM),
new QueryParameter("limit", limit),
new QueryParameter("page", page - 1));

var rows = Database.ExecuteQuery(query,
new QueryParameter("accountId", accountId),
new QueryParameter("orig_lat", latitude),
new QueryParameter("orig_lon", longitude),
new QueryParameter("dist", 1000 * rangeInKM),
new QueryParameter("limit", limit),
new QueryParameter("page", page - 1));

关于c# - MySqlDataReader 没有读取我的查询,就好像没有返回行一样,但 PHPMyAdmin 返回具有相同查询的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38680322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com