• Unity3D 连接MySql


    MySql安装如下:

    https://www.cnblogs.com/dlvguo/p/9671832.html

    Unity连接MySql首先要在Unity的安装目录D:Unity2017EditorDataMonolibmono2.0引用4个dll文件:

    I18N.CJK.dll、I18N.dll、I18N.West.dll、System.data.dll

    同时去Mysql官网下载Mysql.data.dll 注意下载的dll要跟unity .net版本一直 一般用3.5。

    发布的时候在PlayerSetting要设置Api Compatibility Level为.net 2.0不能使用简版的.net 2.0 Subset否则导致发布失败。

    在Unity目录下添加Plugins文件夹,将dll放进去 unity将会自动引用。

    这边做一个查询连接表的Test,写SqlHelper:

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 using MySql.Data;
     5 using MySql.Data.MySqlClient;
     6 using System;
     7 public class SqlHelper {
     8     private static SqlHelper _instance = new SqlHelper();
     9     private static string connStr="Database=Lwcc;Data Source = 172.22.224.87; User Id = root; Password=123456;port=3306";
    10     private string ip;
    11     private string acc;
    12     private string psd;
    13     private string db;
    14     public static SqlHelper Insatance { get
    15         {
    16             return _instance;
    17         } }
    18     /// <summary>
    19     /// 获取连接
    20     /// </summary>
    21     /// <returns></returns>
    22     private MySqlConnection GetSqlConn()
    23     {
    24         MySqlConnection sqlConn = null;
    25         sqlConn = new MySqlConnection(connStr);
    26         return sqlConn;
    27     }
    28     /// <summary>
    29     /// 获取所有表名
    30     /// </summary>
    31     /// <returns></returns>
    32     public IEnumerable<string> GetTables()
    33     {
    34         string sql = string.Format(@"select table_name from information_schema.tables where table_schema='{0}' and table_type='base table'", db);
    35         //string sql = @"select table_name from information_schema.tables where table_schema='lwcc' and table_type='base table'";
    36         List<string> strs = new List<string>();
    37         var conn = GetSqlConn();
    38         conn.Open();
    39         MySqlCommand cmd = new MySqlCommand(sql, conn);
    40         MySqlDataReader reader = cmd.ExecuteReader();
    41         while (reader.Read())
    42         {
    43             strs.Add(reader[0].ToString());
    44         }
    45         conn.Close();
    46         return strs;
    47     }
    48     /// <summary>
    49     /// 设置连接Str
    50     /// </summary>
    51     /// <param name="ip"></param>
    52     /// <param name="acc"></param>
    53     /// <param name="psd"></param>
    54     /// <param name="db"></param>
    55     public void SetConnStr(string ip,string acc,string psd,string db)
    56     {
    57         this.ip = ip;
    58         this.acc = acc;
    59         this.psd = psd;
    60         this.db = db;
    61         connStr = string.Format("DataBase={0};Data Source= {1};User Id = {2};Password={3};port=3306;", db, ip, acc, psd);
    62     }
    63     public void ClearTables(IEnumerable<string> tbs)
    64     {
    65         string cmdText = "";
    66         foreach(var item in tbs)
    67         {
    68             cmdText += string.Format("truncate table {0};", item);
    69         }
    70         var conn = GetSqlConn();
    71         conn.Open();
    72         MySqlCommand cmd = new MySqlCommand(cmdText,conn);
    73         cmd.ExecuteNonQuery();
    74         conn.Close();
    75     }
    76 }

    附上源码,这是我用Unity制作的一个Mysql删表小工具,能够查询所有表,并清空。

    https://github.com/dlvguo/SimpleMysqlTool 

  • 相关阅读:
    easyui datagrid 遇到的坑 cannot read property ·· pageNum bug and so on
    原生js上传图片时的预览
    yield的理解
    js时间格式化
    js 实现复制粘贴时注意方法中需要两次点击实现的bug
    jquery 页面分页的实现
    easyui dialog 表单提交,弹框初始化赋值,dialog实现
    a标签(普通标签如span)没有disabled属性 ,怎样利用js实现该属性
    js之数据类型(对象类型——构造器对象——数组2)
    js之数据类型(对象类型——构造器对象——数组1)
  • 原文地址:https://www.cnblogs.com/dlvguo/p/10031417.html
Copyright © 2020-2023  润新知