网上商城项目(数据库、Unity)
1.导入Unity连接数据库的插件(插件已上传,有需要的同学可以自行下载)
2.先创建一个工具类用于数据库的连接、更新、读取、以及数据库的连接、读取的关闭(这样就可以不用每次都写,减少了代码的冗余,需要使用数据库操作的时候继承这个工具类就可以了)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MySql.Data.MySqlClient;
/*
*文件描述:
*创始人:
*创建时间:
*修改时间:
*版本:1.0
*/
public class Tools
{
MySqlConnection con = null;
//1.连接数据库方法
public MySqlConnection Conncet()
{
string a = "server=localhost;database=onlineshopping;userid=root;password=root;";
con = new MySqlConnection(a);
con.Open();
// Close(con);us
return con;
}
//2.增删改方法
public int PublicUpdate(string sql)
{
con = Conncet();
MySqlCommand com = new MySqlCommand(sql, con);
int i = com.ExecuteNonQuery();
//Close(con);
Close();
return i;
}
//3.查询方法
public MySqlDataReader PublicSelect(string sql)
{
con = Conncet();
MySqlCommand com = new MySqlCommand(sql,con);
MySqlDataReader reader = com.ExecuteReader();
return reader;
}
//4.关闭(增删改)
public void Close() {
if (con!=null)
{
con.Close();
}
}
//关闭(查询、连接)
public void Close(MySqlDataReader reader) {
if (reader!=null)
{
reader.Close();
}
if (con!=null)
{
con.Close();
}
}
}
3.搭建UI(UI的顺序自行调整一下)
3.1 UserRegPanel
3.2AdminloginPanel
3.3AddProductPanel
3.4SelectOrderPanel
3.5DeleteOrderPanel
3.6DeleteProductPanel
3.7ChangeProcuctsPanel
3.8ChangeProcuctsPanel
3.9OthersButtonsPanel
3.10ShoppingPanel
3.11SelectProductsPanel
3.12UserLogin
4.在Mysql中建数据库以及表等等
4.1创建onlineshopping数据库
4.2创建administrator表
4.3创建orders表
4.4创建product表
4.5创建user表