Unity连接数据库操作基础

首先找到这两个东西

在Unity中的Assets文件夹新建一个名叫Plugins的子文件

将上面的两个插件加载进这个文件

 Unity中数据库操作

先引入using MysqL.Data.MysqLClient;这个空间

public class mytest : MonoBehavIoUr {

	
	void Start () {
		Ping();
	}
	void Ping () {
		//表示服务器和所要操作的数据库用户名和密码
		string a = "server=localhost;database=teacher;userid=root;password=root;";
		MysqLConnection con = new MysqLConnection(a);

		//打开连接
		con.open();

		//增加
		//string sql = "insert into xinxi(name,sex,age,salary) values('邱龙华','男',21,90000)";

		//删除
		//string sql = "delete from xinxi where id=4";

		//修改
		//string sql = "update xinxi set name='彭锦涛' where id=3";
		string sql = "select * from xinxi";
		MysqLCommand com = new MysqLCommand(sql,con);
		//     if (com.ExecuteNonQuery()>0)
		//     {
		//print("OK");
		//     }
		//     else
		//     {
		//print("NO");
		//     }

		//查询
		MysqLDataReader read = com.ExecuteReader();
		//要查询的数据
		
        while (read.Read())
        {
			int id = read.GetInt32("id");
			string name = read.GetString("name");
			string sex = read.GetString("sex");
			int age = read.GetInt32("age");
			float salary = read.GetFloat("salary");
			//打印读取的数据
			print(id+"\t"+name+"\t"+sex+"\t"+age+"\t"+salary);
		}

		//关闭(释放)资源
		read.Close();
		con.Close();
	}
}

相关文章

实现Unity AssetBundle资源加载管理器 AssetBundle是实现资源...
Unity3D 使用LineRenderer绘制尾迹与虚线 1.添加LineRendere...
Unity 添加新建Lua脚本选项 最近学习Unity的XLua热更新框架的...
挂载脚本时文件名和类名的关联方式 写过Unity脚本的人应该都...
Unity单例基类的实现方式 游戏开发的过程中我们经常会将各种...
这篇文章主要介绍了Unity游戏开发中外观模式是什么意思,具有...