我如何将 azure MySQL 服务器与我的 Xamarin 表单应用程序连接起来?

问题:
我有一个旧的 C# Windows 窗体应用程序,它使用 Microsoft azure (MySQL) 作为数据库服务器,我想构建一个 android 应用程序(使用 Xamarin 窗体),它从相同的 MySQL 服务器 (Azure) 获取数据。 C# WFA 应用程序。

问题:
Xamarin Forms 是否提供了这样的方式来做到这一点?

有没有更好的方法可以让我(作为初学者)在两个不同的平台(在我的情况下是桌面和手机)上与两个不同的应用程序共享同一个数据库?

提前致谢


MMTTMM
浏览 81回答 1
1回答

繁星淼淼

首先,欢迎来到SO!Xamarin Forms 是否提供这种方式来做到这一点?简单的答案是(不幸的是):不。如果要使用 Xamarin Forms 重用存储在 Azure 上的完全相同的数据库,则应通过 Web 公开与 Azure 数据库交互的 API。然后,您的 Xamarin Forms 应用程序将使用类连接到您的 API&nbsp;HttpClient,以便发布/获取数据。Xamarin 表单支持本地 SQLite 数据库引擎,仅此而已。如果需要,请在此处了解有关使用 Xamarin 表单的本地数据库的更多信息。有没有更好的方法可以让我(作为初学者)在两个不同的平台(在我的情况下是桌面和手机)上与两个不同的应用程序共享同一个数据库?最好的方法是明确公开链接到 Azure DB 的 Web API。将来,您的桌面和移动应用程序(以及任何其他应用程序)将调用您的 ApiController 方法,这意味着这将是您所有应用程序之间的共享代码。下面是一个关于如何从 Xamarin Forms 调用 API 的通用示例方法:public static async Task<TResult> GetData<TResult>(string apiTarget){&nbsp; &nbsp; &nbsp; using (var client = new HttpClient())&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//setup client&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;client.BaseAddress = new Uri(_API_BASE_URI);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;client.MaxResponseContentBufferSize = 9999999;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;client.DefaultRequestHeaders.Accept.Clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _TOKEN);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TResult _Result = default(TResult);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//make request&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HttpResponseMessage response = await client.GetAsync(apiTarget).ConfigureAwait(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (response.IsSuccessStatusCode)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_Result = (TResult)JsonConvert.DeserializeObject<TResult>(content);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return _Result;&nbsp; &nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP