纳金网
标题:
连接MySQL数据库测试通过,获取数据成功_教程+测试
[打印本页]
作者:
她。
时间:
2012-6-29 14:13
标题:
连接MySQL数据库测试通过,获取数据成功_教程+测试
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";//Mysql1001.webweb.com
static string port_="";//4306
static string id = "root";//98ceef_dx15_1
static string pwd = "mysql!";//yusiyuanlove
static string database = "test";//db_98ceef_dx15_1
static string result = "";
private string strCommand = "SELECT * FROM test1";//mac_uc_members
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 = {2}; User ID = {3}; Password = {4};",host,port_,database,id,pwd);//Port={1};
openSqlConnection(connectionString);
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 + "
" + ee.Message.ToString());
}
return ds;
}
#endregion
}
复制代码
另一段代码:
using UnityEngine;
using System;
using System.Collections;
using System.Data;
public class DataBaseTest : MonoBehaviour {
public GUISkin myGUISkin = new GUISkin();
string strID = "";
string strName = "";
string strSex = "";
int Index = 1;
// Use this for initialization
void Start () {
}
void OnGUI()
{
GUI.skin = myGUISkin;
if (GUI.Button(new Rect(100,320,100,100),"Click Me"))
{
foreach(DataRow dr in CMySql.MyObj.Tables[0].Rows)
{
if (Index.ToString() == dr["id"].ToString())
{
strID = dr["id"].ToString();
strName = dr["name"].ToString();
strSex = dr["age"].ToString();
break;
}
}
Index++;
if(Index > 5)
{
Index = 1;
}
}
GUI.Label(new Rect(320,100,150,70),"DataBaseTest");
GUI.Label(new Rect(300,210,150,70),strID);
GUI.Label(new Rect(300,320,150,70),strName);
GUI.Label(new Rect(300,430,150,70),strSex);
}
}
复制代码
这是连接本地数据库的代码。
作者:
王者再临
时间:
2012-12-30 04:15
学习了,虽然还是有难度,谢谢楼主的用心
作者:
狂风大尉
时间:
2012-12-31 00:39
谢谢楼主的帖子分享,学习了
欢迎光临 纳金网 (http://wwww.narkii.com/club/)
Powered by Discuz! X2.5