Unity3D开发(常用方法之mysql增删改查)

using MySql.Data.MySqlClient;

using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

using UnityEngine;



public class MysqlDataCompile : MonoBehaviour {

public int id;

public string modelname;

public double width;

public double heigh;





void Start()

{

try

{

//数据库的增删改查

string MySqlStr = "Database=;server=;User Id=;Password=;";



MySqlConnection mySqlcon = new MySqlConnection(MySqlStr);//连接数据库;

mySqlcon.Open();



//查

string str = "select * from furnituretest where id=" + id + ";";

MySqlCommand mySqlCom = new MySqlCommand(str, mySqlcon);

CheckedMySql(mySqlCom);



//增

//str = "insert into furnituretest values(" + id + ",'" + modelname + "'," + width + ",'" + heigh + "');";

//mySqlCom = new MySqlCommand(str, mySqlcon);

//IducMySql(mySqlCom);



//删

//str = "delete from furnituretest where id=5;";

//mySqlCom = new MySqlCommand(str, mySqlcon);

//IducMySql(mySqlCom);



// 改

//str = "update furnituretest set width=" + width + " where id=" + id + ";";

//mySqlCom = new MySqlCommand(str, mySqlcon);

//IducMySql(mySqlCom);



//GetId(7);

}

catch (Exception e)

{

throw new Exception("服务器连接失败,请重新检查是否打开MySql服务。" + e.Message.ToString());

}

}



//(查)

static private void CheckedMySql(MySqlCommand mySqlcom)

{

MySqlDataReader read = mySqlcom.ExecuteReader();

try

{

if (read.Read())

{



if (read.HasRows)//查找

{

Debug.Log("id:" + read.GetInt32("id"));

Debug.Log("modelname:" + read.GetString("modelname"));

Debug.Log("width:" + read.GetDouble("width"));

Debug.Log("height:" + read.GetDouble("height"));

}



}

}

catch (Exception e)

{



throw e;

}

finally

{

read.Close();

}







}





//增 删 改 查

static private void IducMySql(MySqlCommand mySqlcom)

{

try

{

mySqlcom.ExecuteNonQuery();

}

catch (Exception e)

{

Debug.Log("已存在");

//throw e;

}



}





//指定ID参数打印相关信息

static private void GetId(int id)

{



string MySqlStr = "Database=;server=;User Id=;Password=;";

MySqlConnection mySqlcon = new MySqlConnection(MySqlStr);

mySqlcon.Open();

string str = "select * from furnituretest where id=" + id;//注意 别把分号打上去;

MySqlCommand mySqlCom = new MySqlCommand(str, mySqlcon);

MySqlDataReader reader = mySqlCom.ExecuteReader();

try

{

while (reader.Read())

{



if (reader.HasRows)//查找

{

Debug.Log("id:" + reader.GetInt32("id"));

Debug.Log("modelname:" + reader.GetString("modelname"));

Debug.Log("width:" + reader.GetDouble("width"));

Debug.Log("height:" + reader.GetDouble("height"));

}



}



}

catch (Exception e)

{



throw e;

}

finally

{

reader.Close();

}

}

}

 unity中需要导入五个dll:

Unity\Editor\Data\Mono\lib\mono\unity

目录下的

I18N.dll   I18N.West.dll   I18N.CJK.dll 

 

Unity\Editor\Data\Mono\lib\mono\2.0

目录下的

System.Data.dll  System.Drawing.dll  

还需要 mysql5.0版本以上的  

MySql.Data.dll

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萌城的小小少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值