这个是根据前面别人的例子改了一下的,呵呵~~~
注:这里面的System.Data.dll 是Unity\Editor\Data\Mono\lib\mono\2.0\System.Data.dll;但是MySql.data.dll的版本我也不知道怎么来判断,我本来用的是1.0.6.15336这个版本的,但是一直报错“MySqlException: Character set 'gbk' is not supported”,报这个错误一般都是MySql.data.dll的版本太低了,后来我改成5.0的了,结果就ok了。反正我传到我的资源里面了,不行的话你们一个个试。
数据库创建如下:
- set feedback off;
- drop database db;
- create database db;
- USE DB;
- drop table s;
- create table s(
- sno Int(10) not null primary key,
- name varchar(10),
- sex varchar(10)
- );
- insert into s values('001','杨纯','男');
- insert into s values('002','yaya','女');
- insert into s values('003','dlnuchunge','男');
- select * from s;
unity3d里面的代码如下:
- using UnityEngine;
- using System;
- using System.Collections;
- using System.Data;
- using MySql.Data.MySqlClient;
- public class CMySql : MonoBehaviour {
- // Global variables
- public static MySqlConnection dbConnection;//Just like MyConn.conn in StoryTools before
- static string host = "localhost";
- static string id = "root";
- static string pwd = "mysql";
- static string database = "db";
- static string result = "";
- private string strCommand = "Select sno from s ;";
- public static DataSet MyObj;
- void OnGUI()
- {
- host = GUILayout.TextField( host, 200, GUILayout.Width(200));
- id = GUILayout.TextField( id, 200, GUILayout.Width(200));
- pwd = GUILayout.TextField( pwd, 200, GUILayout.Width(200));
- if(GUILayout.Button("Test"))
- {
- string connectionString = string.Format("Server = {0}; Database = {1}; User ID = {2}; Password = {3};",host,database,id,pwd);
- openSqlConnection(connectionString);
- MySqlCommand mySqlCommand = new MySqlCommand("Select * from s;", dbConnection);
- MySqlDataReader reader = mySqlCommand.ExecuteReader();
- try
- {
- while (reader.Read())
- {
- if (reader.HasRows)
- {
- print("编号:" + reader.GetInt32(0)+"|姓名:"+reader.GetString(1)+"|性别:"+reader.GetString(2));
- }
- }
- }
- catch (Exception)
- {
- Console.WriteLine("查询失败了!");
- }
- finally
- {
- reader.Close();
- }
- MyObj = GetDataSet(strCommand);
- }
- GUILayout.Label(result);
- }
- // On quit
- public static void OnApplicationQuit() {
- closeSqlConnection();
- }
- // Connect to database
- private static void openSqlConnection(string connectionString) {
- dbConnection = new MySqlConnection(connectionString);
- dbConnection.Open();
- result = dbConnection.ServerVersion;
- //Debug.Log("Connected to database."+result);
- }
- // Disconnect from database
- private static void closeSqlConnection() {
- dbConnection.Close();
- dbConnection = null;
- //Debug.Log("Disconnected from database."+result);
- }
- // MySQL Query
- public static void doQuery(string sqlQuery) {
- IDbCommand dbCommand = dbConnection.CreateCommand();
- dbCommand.CommandText = sqlQuery;
- IDataReader reader = dbCommand.ExecuteReader();
- reader.Close();
- reader = null;
- dbCommand.Dispose();
- dbCommand = null;
- }
- #region Get DataSet
- public DataSet GetDataSet(string sqlString)
- {
- //string sql = UnicodeAndANSI.UnicodeAndANSI.UnicodeToUtf8(sqlString);
- 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());
- }
- return ds;
- }
- #endregion
- }
结果如下:
呵呵,大家试试吧~~~~转自 http://blog.csdn.net/nateyang/article/details/7687191