第一次在这里请求您的帮助。
我知道我的标题是错误的,如果我们弄清楚问题出在哪里,我会改变它,此刻我有点迷茫。
好吧,我们首先要从我的数据库中选择当前用户范围内的所有用户。
如果我执行查询,它会从 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()) ... 永远不会“触发”,就好像它们没有与查询匹配的行一样。
现在我有点失去了我应该做些什么来解决这个问题,欢迎所有帮助!
非常感谢你的帮助 :)