日期:2014-05-16 浏览次数:20646 次
<?php $con = mysql_connect("127.0.0.1","root","123"); if (!$con) { die('Could not connect: ' . mysql_error()); } ?>
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 = "127.0.0.1"; static string id = "root"; static string pwd = "123"; static string database = "test"; static string result = ""; private string strCommand = "Select * from unity3d ORDER BY id;"; 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); 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:" + s