【发布时间】:2021-09-01 10:13:27
【问题描述】:
需要将直连数据库替换为API。
我使用这段代码直接连接到 MySQL db 并更改 pin 信息:
public async void DatabaseConnection(List<CustomPin> pins)
{
string ConnectionString = "server=192.168.0.1;uid=user;port=4444;pwd=pass;database=dbName;";
MySqlConnection Conn = new MySqlConnection(ConnectionString);
try
{
Conn.Open();
string query = "SELECT * FROM sel_alert_level s;";
MySqlCommand myCommand = new MySqlCommand(query, Conn);
MySqlDataReader myReader;
myReader = myCommand.ExecuteReader();
try
{
while (myReader.Read())
{
int codeNum = myReader.GetInt32(4);
int level = myReader.GetInt32(3);
int mapCode = myReader.GetInt32(0);
foreach (var item in pins)
{
if (item.CodeNum == codeNum)
{
item.AlertLevel = level;
item.CodeNum = codeNum;
item.MapCode = mapCode;
//await DisplayAlert("Alert", mapCode.ToString(), "ok");
}
}
//await DisplayAlert("Database Connection", "Connected .." + Environment.NewLine + myReader.GetInt32(0) + Environment.NewLine + myReader.GetString(1) + Environment.NewLine + myReader.GetString(2) + Environment.NewLine + myReader.GetInt32(3) + Environment.NewLine + myReader.GetInt32(4), "OK");
}
}
finally
{
myReader.Close();
Conn.Close();
}
}
catch (Exception ex)
{
await DisplayAlert("Database Connection", "Not Connected ..." + Environment.NewLine + ex.ToString(), "OK");
}
}
现在我使用 API Response 创建了相同的方法,以及像DatabaseConnection(); 一样的操作只是尝试更新信息,但对我不起作用:(
public async void APIConnection(List<CustomPin> pins)
{
try
{
WaterBindingData waterData = await _restServiceData.GetWaterDataForecast(GenerateRequestUriStations(Constants.EndPoint), GenerateRequestUri(Constants.EndPoint));
foreach (var water in waterData.WaterStation.Stations)
{
foreach (var item in pins)
{
if (item.CodeNum == water.CodeNum)
{
item.AlertLevel = water.AlertLevelStation;
item.CodeNum = water.CodeNum;
item.MapCode = water.MapCode;
}
}
}
}
catch (Exception ex)
{
await DisplayAlert("Data Alert", "Error:" + Environment.NewLine + ex.ToString(), "OK");
}
}
我这里没有任何错误。 waterData 带有数据,但引脚中的数据没有改变..我不知道为什么......
MapCode and other variables not changed.
我在构造函数中这样调用这两个方法:
DatabaseConnection(customMap.CustomPins);
APIConnection(customMap.CustomPins);
所以...当我启动项目时,我收到如下消息:
/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/Microsoft.Common.CurrentVersion.targets(5,5): Warning MSB3276: Found conflicts between different versions of the same dependent assembly. Please set the "AutoGenerateBindingRedirects" property to true in the project file. For more information, see http://go.microsoft.com/fwlink/?LinkId=294190. (MSB3276) (MaritsaTundzhaForecast.iOS)
我检查了this link,但我没有properties 选项,因为我使用的是mac。我的项目只有options。
这可能不会改变图钉中的内容吗?它不起作用的原因是什么?
我检查了循环中的 if 语句,她工作了:
【问题讨论】:
-
你调试过你的代码吗?你确定
foreach循环和if语句中的代码实际上正在执行吗? -
我更新了问题,看最后一张截图。 if 语句工作正常..
-
sql和webapi唯一的不同就是一个是异步方法,一个不是。
-
有没有办法解决这个问题?
标签: ios google-maps xamarin