我一直在阅读如何完成此任务,但每个人都使用 PHP。 AFAIK 我已经读到可以使用 C# 连接到 Android 项目中的 MySql,但到目前为止我还没有找到任何有用的东西来完成此任务。
最佳答案
如果您正在谈论连接到本地托管或远程的 MySQL 数据库,那么这里是解决方案。首先,您需要在服务器上添加 IP 以允许直接连接,通常是通过 cPanel 允许的 IP 完成的。
using System.Data;
using System;
using MySql.Data.MySqlClient;`enter code here`
//this is the part you will need for your method`enter code here`
private void BtnInsert_Click(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection("Server=yourdomainname.com;Port=3306;database=your_database_name;User Id=yourMySQLuserName;Password=yourpassword;charset=utf8");
try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
txtSysLog.Text = "Successfully connected";
}
}
catch(MySqlException ex)
{
txtSysLog.Text = ex.ToString();
}
finally
{
con.Close();
}
}
txtSysLog
是 Android 应用程序中的文本标签。它仅用于显示连接是否完成。
我建议使用 Restful Web 服务来提高安全性,而不是直接使用 mysql 连接。
关于c# - Android用C#连接Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16798325/