Connect to the database in unity3d

Source: Internet
Author: User
  1. Using system;
  2. Using system. collections;
  3. Using system. Data;
  4. Using mysql. Data. mysqlclient;
  5. Public class cmysql: monobehaviour {
  6. // Global variables
  7. Public static mysqlconnection dbconnection; // just like myconn. Conn in storytools before
  8. Static string host = "192.168.1.100 ";
  9. 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.
  10. Static string Pwd = "123456 ";
  11. Static string database = "test ";
  12. Static string result = "";
  13. Private string strcommand = "select * From unity3d_test order by ID ;";
  14. Public static dataset myobj;
  15. Void ongui ()
  16. {
  17. Host = guilayout. textfield (host, 200, guilayout. Width (200 ));
  18. Id = guilayout. textfield (ID, 200, guilayout. Width (200 ));
  19. Pwd = guilayout. textfield (PWD, 200, guilayout. Width (200 ));
  20. If (guilayout. Button ("test "))
  21. {
  22. String connectionstring = string. format ("Server = {0}; database = {1}; user id = {2}; Password = {3};", host, database, ID, PWD );
  23. Opensqlconnection (connectionstring );
  24. Myobj = getdataset (strcommand );
  25. }
  26. Guilayout. Label (result );
  27. }
  28. // On quit
  29. Public static void onapplicationquit (){
  30. Closesqlconnection ();
  31. }
  32. // Connect to database
  33. Private Static void opensqlconnection (string connectionstring ){
  34. Dbconnection = new mysqlconnection (connectionstring );
  35. Dbconnection. open ();
  36. Result = dbconnection. serverversion;
  37. // Debug. Log ("connected to database." + result );
  38. }
  39. // Disconnect from database
  40. Private Static void closesqlconnection (){
  41. Dbconnection. Close ();
  42. Dbconnection = NULL;
  43. // Debug. Log ("disconnected from database." + result );
  44. }
  45. // MySQL Query
  46. Public static void doquery (string sqlquery ){
  47. Idbcommand dbcommand = dbconnection. createcommand ();
  48. Dbcommand. commandtext = sqlquery;
  49. Idatareader reader = dbcommand. executereader ();
  50. Reader. Close ();
  51. Reader = NULL;
  52. Dbcommand. Dispose ();
  53. Dbcommand = NULL;
  54. }
  55. # Region get Dataset
  56. Public dataset getdataset (string sqlstring)
  57. {
  58. // String SQL = unicodeandansi. unicodeandansi. unicodetoutf8 (sqlstring );
  59. Dataset DS = new dataset ();
  60. Try
  61. {
  62. Mysqldataadapter da = new mysqldataadapter (sqlstring, dbconnection );
  63. Da. Fill (DS );
  64. }
  65. Catch (exception ee)
  66. {
  67. Throw new exception ("SQL:" + sqlstring + "/N" + ee. Message. tostring ());
  68. }
  69. Return Ds;
  70. }
  71. # Endregion
  72. }

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?
  1. Using unityengine;
  2. Using system;
  3. Using system. collections;
  4. Using system. Data;
  5. Public class databasetest: monobehaviour {
  6. Public guiskin myguiskin = new guiskin ();
  7. String Strid = "";
  8. String strname = "";
  9. String strsex = "";
  10. Int Index = 1;
  11. // Use this for initialization
  12. Void start (){
  13. }
  14. Void ongui ()
  15. {
  16. Gui. Skin = myguiskin;
  17. If (GUI. Button (New rect (100,320,100,100), "click me "))
  18. {
  19. Foreach (datarow DR in cmysql. myobj. Tables [0]. Rows)
  20. {
  21. If (index. tostring () = Dr ["ID"]. tostring ())
  22. {
  23. Strid = Dr ["ID"]. tostring ();
  24. Strname = Dr ["name"]. tostring ();
  25. Strsex = Dr ["sex"]. tostring ();
  26. Break;
  27. }
  28. }
  29. Index ++;
  30. If (index> 5)
  31. {
  32. Index = 1;
  33. }
  34. }
  35. Gui. Label (New rect (320,100,150, 70), "databasetest ");
  36. Gui. Label (New rect (300,210,150, 70), Strid );
  37. Gui. Label (New rect (300,320,150, 70), strname );
  38. Gui. Label (New rect (300,430,150, 70), strsex );
  39. }
  40. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.