- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
三、MySql Connector/Net 1.0
http://dev.mysql.com/downloads/connector/net/1.0.html
注意:高版本不能使用;
下载Binaries,no installer版本,解压后在bin文件夹中有MySql.data.dll文件。将此文件拷贝至Assets文件夹下,可以放在任意目录下。然后可在C#的脚本中使用using MySql.Data.MySqlClient;
生成的工程需要将此DLL放置在同级目录下。并可在windows下正常使用;
四、代码
using UnityEngine;
using System;
using System.Collections;
using System.Data;
using MySql.Data.MySqlClient;
public class CMySql : MonoBehaviour {
// Global variables
private static MySqlConnection dbConnection;
static string host = "192.168.1.100";
static string id = "mysql";
static string pwd = "123456";
static string result = "";
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 = "Server="+host+";Database=test;User ID="+id+"assword="+pwd+"ooling=false";
openSqlConnection( connectionString );
}
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;
}
} |
|