我在机器上安装了mysql。
我安装了 GenyMotion Android 模拟器
我在 Xamarin 应用程序中使用 Xamarin MySQL 连接器。
如何从 Genymotion 中运行的 Xamarin 应用程序连接到 mysql 数据库?
请注意,由于显而易见的原因,使用 127.0.0.1 作为 IP 地址是行不通的。
最佳答案
您的应用不应直接与数据库服务器通信。通过 API 是更好的方法。
现在我们已经解决了这个问题,这是一个取自 here 的简短示例。 :
try
{
string connectionString = "Server=your.ip.address;Port=3306;database=YOUR_DATA_BASE;User Id=root;Password=password;charset=utf8";
MySqlConnection sqlconn = new MySqlConnection(connectionString);
sqlconn.Open();
string queryString = "select count(0) from ACCOUNT";
MySqlCommand sqlcmd = new MySqlCommand(queryString, sqlconn);
String result = sqlcmd.ExecuteScalar().ToString();
// do something with the results
sqlconn.Close();
} catch (Exception ex)
{
Console.WriteLine (ex.Message);
}
您应该能够从 GenyMotion(使用 VirtualBox)创建的虚拟网络适配器获取要使用的 IP 地址
关于c# - 如何从 Genymotion 连接到 MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37271692/