Reputation: 303
I am required to develop a cross-platform app for use within an internal network. The app must connect to a MySQL database within that network. I must do this without touching any server on the network (I cannot setup web APIs within the network). I have chosen Xamarin.Forms for development.
I searched for ways to integrate MySQL directly with Xamarin.Forms and most people rightly recommended to use web APIs. Unfortunately, that's not possible in my case. In some answers here on SO, I found the Nuget package Xamarin.MySql.Data
.
However, when I add it to the project, build fails with this error:
NU1202 Package Xamarin.MySql.Data 1.0.1 is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299) / win10-arm. Package Xamarin.MySql.Data 1.0.1 supports:
- monoandroid (MonoAndroid,Version=v0.0)
- monotouch (MonoTouch,Version=v0.0)
- xamarinios (Xamarin.iOS,Version=v0.0)
I understand that this is a compatibility issue.
What other options are available? Thanks.
Upvotes: 3
Views: 18162
Reputation: 9723
According to the official documentation of the MySQL Connector for .NET it runs on any version of .NET, hence you should be able to use it from a Xamarin.Forms app.
MySQL Connector/NET runs on any platform that supports the .NET Standard (.NET Framework, .NET Core, and Mono) (see the documentation)
You may install the connector from NuGet and then start using it.
If you are used to ADO.NET (or willing to learn it), you can then start using the MySqlConnection
class. Please see the following very basic example:
List<MyObject> result = new List<MyObject>();
using var connection = new MySqlConnection(connectionString); // C# 8!
connection.Open();
using var command = connection.CreateCommand();
command.CommandText = "SELECT id, name FROM fooBar WHERE deleted = 0"; // whatever
using var reader = command.ExecuteReader();
while(reader.Read())
{
result.Add(new MyObject
{
Id = reader.GetUInt64("id"), // the parameter is the name of the column
Name = reader.GetString("name"),
});
}
return result;
(see this page on connection strings if you don't have any idea how connectionString
shall look like. Please note that the MySQL instance has to be accessible from the devices that shall use this connection.)
If you prefer using EF, you'll have to install this NuGet package.
Upvotes: 3