Unity3D 连接MySql

MySql安装如下:

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

Unity连接MySql首先要在Unity的安装目录D:\Unity2017\Editor\Data\Mono\lib\mono\2.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     /// 
19     /// 获取连接
20     /// 
21     /// 
22     private MySqlConnection GetSqlConn()
23     {
24         MySqlConnection sqlConn = null;
25         sqlConn = new MySqlConnection(connStr);
26         return sqlConn;
27     }
28     /// 
29     /// 获取所有表名
30     /// 
31     /// 
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     /// 
49     /// 设置连接Str
50     /// 
51     /// 
52     /// 
53     /// 
54     /// 
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 

转载于:https://www.cnblogs.com/dlvguo/p/10031417.html

你可能感兴趣的:(数据库,游戏)