- 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 = "192.168.1.100 ";
- Static string id = "MySQL"; // here is the username of your own database. I wanted to use root at first and found that it was not enough. Later I added a new user.
- Static string Pwd = "123456 ";
- Static string database = "test ";
- Static string result = "";
- Private string strcommand = "select * From unity3d_test 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:" + sqlstring + "/N" + ee. Message. tostring ());
- }
- Return Ds;
- }
- # Endregion
- }
Using unityengine; <br/> using system. collections; <br/> using system. data; <br/> using MySQL. data. mysqlclient; <br/> public class cmysql: monobehaviour {<br/> // global variables <br/> Public static mysqlconnection dbconnection; // just like myconn. conn in storytools before <br/> static string host = "192.168.1.100"; <br/> static string id = "MySQL"; // The username of your own database, I first wanted to use root and found that it was not feasible. Later I added a new user before I could <br/> static string Pwd = "123456 "; <br/> static string database = "test"; <br/> static string result = ""; </P> <p> private string strcommand = "select * From unity3d_test order by ID;"; <br/> Public static dataset myobj; <br/> void ongui () <br/> {<br/> host = guilayout. textfield (host, 200, guilayout. width (200); <br/> id = guilayout. textfield (ID, 200, guilayout. width (200); <br/> Pwd = guilayout. textfield (PWD, 200, guilayout. width (200); <br/> If (guilayout. button ("test") <br/>{< br/> string connectionstring = string. format ("Server = {0}; database = {1}; user id = {2}; Password = {3};", host, database, ID, PWD ); <br/> opensqlconnection (connectionstring); </P> <p> myobj = getdataset (strcommand); <br/>}< br/> guilayout. label (result); <br/>}< br/> // on quit <br/> Public static void onapplicationquit () {<br/> closesqlconnection (); <br/>}</P> <p> // connect to database <br/> Private Static void opensqlconnection (string connectionstring) {<br/> dbconnection = new mysqlconnection (connectionstring); <br/> dbconnection. open (); <br/> result = dbconnection. serverversion; <br/> // debug. log ("connected to database. "+ result); <br/>}</P> <p> // disconnect from database <br/> Private Static void closesqlconnection () {<br/> dbconnection. close (); <br/> dbconnection = NULL; <br/> // debug. log ("disconnected from database. "+ result); <br/>}</P> <p> // MySQL query <br/> Public static void doquery (string sqlquery) {<br/> idbcommand dbcommand = dbconnection. createcommand (); <br/> dbcommand. commandtext = sqlquery; <br/> idatareader reader = dbcommand. executereader (); <br/> reader. close (); <br/> reader = NULL; <br/> dbcommand. dispose (); <br/> dbcommand = NULL; <br/>}< br/> # region get dataset <br/> Public dataset getdataset (string sqlstring) <br/>{< br/> // string SQL = unicodeandansi. unicodeandansi. unicodetoutf8 (sqlstring); </P> <p> dataset DS = new dataset (); <br/> try <br/> {<br/> mysqldataadapter da = new mysqldataadapter (sqlstring, dbconnection); <br/> da. fill (DS); </P> <p >}< br/> catch (exception ee) <br/>{</P> <p> throw new exception ("SQL:" + sqlstring + "/N" + ee. message. tostring (); <br/>}< br/> return Ds; </P> <p >}< br/> # endregion <br/>}
C # code:
[C-sharp]
View plaincopyprint?
- 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 ["sex"]. 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 );
- }
- }
Using unityengine; <br/> using system. collections; <br/> using system. data; <br/> public class databasetest: monobehaviour {<br/> Public guiskin myguiskin = new guiskin (); <br/> string Strid = ""; <br/> string strname = ""; <br/> string strsex = ""; <br/> int Index = 1; <br/> // use this for initialization <br/> void start () {<br/>}< br/> void ongui () <br/> {<br/> GUI. skin = myguiskin; <br/> If (GUI. button (New rect (100,320,100,100), "click me") <br/>{< br/> foreach (datarow DR in cmysql. myobj. tables [0]. rows) <br/>{< br/> If (index. tostring () = Dr ["ID"]. tostring () <br/>{< br/> Strid = Dr ["ID"]. tostring (); <br/> strname = Dr ["name"]. tostring (); <br/> strsex = Dr ["sex"]. tostring (); </P> <p> break; <br/>}< br/> index ++; <br/> If (index> 5) <br/> {<br/> Index = 1; <br/>}</P> <p >}< br/> GUI. label (New rect (320,100,150, 70), "databasetest"); <br/> GUI. label (New rect (300,210,150, 70), Strid); <br/> GUI. label (New rect (300,320,150, 70), strname); <br/> GUI. label (New rect (300,430,150, 70), strsex); </P> <p >}< br/>}
2. Load the DLL
Import mysql. Data. DLL to assets, and then to unity/Editor/data/frameworks/mono. framework.
Import system. Data. DLL to assets together. Of course, if you want to show Chinese, please refer to Chinese tutorial and create a guiskin and font
3. Create a data volume content
This is mainly because of the content in the agent
Static string host = "192.168.1.100 ";
Static string id = "MySQL ";
Static string Pwd = "123456 ";
Static string database = "test ";
Private string strcommand = "select * From unity3d_test order by ID ;";
Set the host, ID, and pwd on your own. The simple statement is to import data into your MySQL instance ~
Then create a database named test, create a External table under this test, and name it unity3d_test,
Next, set the unity3d_test parameters to ID, name, and sex (remember to set the ID to primary key and the default value to 1)
Then fill in 5 RMB (the reason for the 5 RMB is that the cost is set to 5 RMB for a cycle, you can change the current quota of your account)
4. Create a gameobject
After creating a gameobject, upload the above two versions. If you have created a guiskin, remember to specify the guiskin
5. Merge rows
Click test to compute the data volume, and then click "Click me" to display the content in the data volume.
6. Summary
The preceding section describes how to export data to a data volume and then display the data in dataset mode to the interface, we welcome you to give me more suggestions.