文章目录
写在前面
版本兼容问题走了许多弯路,令人非常火大。软件的更新,但是配套和支持往往没有跟上,甚至反而不兼容旧版本,而网上多数博客已经不再适用,故执笔记录,希望可以帮助到你。
相关安装包已上传网盘。
链接:https://pan.baidu.com/s/16cNlfe8_XTrW4p3HfgLUNQ
提取码:pdy1
Mysql
安装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 Studio
Visual Studio2022暂不支持Mysql!!!
或者Mysql暂不支持Visual Studio2022!!!
直接劝退到2019
视图->服务器资源管理器
unity
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
usingSystem;usingSystem.Data;usingMySql.Data.MySqlClient;usingUnityEngine;usingSystem.Text;publicclassSqlAccess{publicstaticMySqlConnection dbConnection;publicSqlAccess(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 =newMySqlConnection(connectionString);
dbConnection.Open();
Debug.Log("连接成功!");}catch(Exception e){thrownewException("连接失败!"+ e.Message.ToString());}}//关闭连接publicvoidClose(){if(dbConnection !=null){
dbConnection.Close();
dbConnection.Dispose();
dbConnection =null;}}//查询publicDataSetSelectWhere(string tableName,string[] items,string[] col,string[] operation,string[] values){if(col.Length != operation.Length || operation.Length != values.Length)thrownewException("col.Length != operation.Length != values.Length");StringBuilder query =newStringBuilder();
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());returnExecuteQuery(query.ToString());}//执行sql语句publicstaticDataSetExecuteQuery(string sqlString){if(dbConnection.State == ConnectionState.Open){DataSet ds =newDataSet();try{MySqlDataAdapter da =newMySqlDataAdapter(sqlString, dbConnection);
da.Fill(ds);}catch(Exception ee){thrownewException("SQL:"+ sqlString +"/n"+ ee.Message.ToString());}finally{}return ds;}returnnull;}}
TestSql.cs
usingSystem.Data;usingUnityEngine;publicclassTestSql:MonoBehaviour{publicstring host ="localhost";publicstring port ="3306";publicstring username ="root";publicstring pwd ="123456";publicstring database ="demo";voidStart(){SqlAccess sql =newSqlAccess(host, port, username, pwd, database);string[] items ={"name"};//字段名string[] col ={};string[] op ={};string[] val ={};DataSet ds = sql.SelectWhere("test", items, col, op, val);//替换表名testif(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/
来都来了,不评论两句吗👀
如果文章对你有帮助,记得一键三连❤
版权归原作者 吾仄lo咚锵 所有, 如有侵权,请联系我们删除。