unity连接mysql数据库-最新详细
文章目录
版本兼容问题走了许多弯路,令人非常火大。软件的更新,但是配套和支持往往没有跟上,甚至反而不兼容旧版本,而网上多数博客已经不再适用,故执笔记录,希望可以帮助到你。
相关安装包已上传网盘。
链接:https://pan.baidu.com/s/16cNlfe8_XTrW4p3HfgLUNQ
提取码:pdy1
安装Mysql及相关组件,官网/网盘都可。
https://dev.mysql.com/downloads/
MySQL Installer for Windows
安装Mysql8.0,已安装可跳过此步,相关教程繁多,不再赘述。
https://dev.mysql.com/downloads/installer/
Connector/NET
版本兼容问题,这里用的是v6.3.9
https://downloads.mysql.com/archives/c-net/
MySQL for Visual Studio
https://dev.mysql.com/downloads/windows/visualstudio/
(
插播反爬信息)博主CSDN地址:https://wzlodq.blog.csdn.net/
Visual Studio2022暂不支持Mysql!!!
或者Mysql暂不支持Visual Studio2022!!!
直接劝退到2019
视图->服务器资源管理器
MySql.Data.dll
unity2020已经内置了System.Data.dll和System.Drawing.dll,只需导入MySql.Data.dll即可。
该文件在前面Connector/NET安装目录下(也已上传网盘),如:
C:\Program Files (x86)\MySQL\MySQL Connector Net 6.3.9\Assemblies\v4.0
测试脚本
新建两个脚本测试连接:
SqlAccess.cs
using System;using System.Data;using MySql.Data.MySqlClient;using UnityEngine;using System.Text;public class SqlAccess{public static MySqlConnection dbConnection;public SqlAccess(string host, string port, string username, string pwd, string database){//连接数据库try{string connectionString = string.Format("server = {0};port={1};database = {2};user = {3};password = {4};", host, port, database, username, pwd);Debug.Log(connectionString);dbConnection = new MySqlConnection(connectionString);dbConnection.Open();Debug.Log("连接成功!");}catch (Exception e){throw new Exception("连接失败!" + e.Message.ToString());}}//关闭连接public void Close(){if (dbConnection != null){dbConnection.Close();dbConnection.Dispose();dbConnection = null;}}//查询public DataSet SelectWhere(string tableName, string[] items, string[] col, string[] operation, string[] values){if (col.Length != operation.Length || operation.Length != values.Length)throw new Exception("col.Length != operation.Length != values.Length");StringBuilder query = new StringBuilder();query.Append("SELECT ");query.Append(items[0]);for (int i = 1; i < items.Length; ++i){query.Append(", ");query.Append(items[i]);}query.Append(" FROM ");query.Append(tableName);query.Append(" WHERE 1=1");for (int i = 0; i < col.Length; ++i){query.Append(" AND ");query.Append(col[i]);query.Append(operation[i]);query.Append("'");query.Append(values[0]);query.Append("' ");}Debug.Log(query.ToString());return ExecuteQuery(query.ToString());}//执行sql语句public static DataSet ExecuteQuery(string sqlString){if (dbConnection.State == ConnectionState.Open){DataSet ds = new DataSet();try{MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection);da.Fill(ds);}catch (Exception ee){throw new Exception("SQL:" + sqlString + "/n" + ee.Message.ToString());}finally{}return ds;}return null;}}
TestSql.cs
using System.Data;using UnityEngine;public class TestSql : MonoBehaviour{ public string host = "localhost"; public string port = "3306"; public string username = "root"; public string pwd = "123456"; public string database = "demo"; void Start() { SqlAccess sql = new SqlAccess(host, port, username, pwd, database); string[] items = { "name" };//字段名 string[] col = { }; string[] op = { }; string[] val = { }; DataSet ds = sql.SelectWhere("test", items, col, op, val);//替换表名test if (ds != null) { DataTable table = ds.Tables[0]; foreach (DataRow row in table.Rows) { string str = ""; foreach (DataColumn column in table.Columns) str += row[column] + " "; Debug.Log(str); } } }}
测试结果
新建GameObject,将TestSql脚本绑定上去,记得修改TestSql密码数据库名字段名等。
运行测试:
原创不易,请勿转载(
本不富裕的访问量雪上加霜)
博主首页:https://wzlodq.blog.csdn.net/
来都来了,不评论两句吗👀
如果文章对你有帮助,记得一键三连❤
来源地址:https://blog.csdn.net/qq_45034708/article/details/122618777
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341