c# - Android用C#连接Mysql

标签 c# android mysql

我一直在阅读如何完成此任务,但每个人都使用 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/

相关文章:

c# - HKLM\Software 下的 OpenSubKey 返回 null

android - 适用于 Android 和 IOS 的乐谱 API?

php - 如何将确切的数据库时间戳与我的日期匹配

c# - resharper set 在多个文件上使用

c# - 模型值在回发期间丢失

android - SQL数据库到android SQLite数据库

android - android 中用于确认代码的虚线输入字段

php - 使用 PHP 在 HTML 表中显示图像 [path] 和数据 [Varchar]

mysql - 列数据太长 - Rails 4 迁移已取消

c# - 错误: String was not recognised as a valid date time