Android connect to Mysql with C#

本文关键字:with Mysql to connect Android | 更新日期: 2023-09-27 18:15:49

我一直在阅读如何实现这一点,但每个人都使用PHP。我已经读到有可能在Android项目中使用c#连接到MySql,但到目前为止,我还没有发现任何有用的东西来完成这一点。

Android connect to Mysql with C#

如果您正在谈论连接到本地或远程托管的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。