Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.19 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using MySql.Data.MySqlClient;
  4. using MySql;
  5. using MySql.Data;
  6. using MySql.Data.Types;
  7. using System.Diagnostics;
  8. using System.Data;
  9.  
  10. public class NewBehaviourScript : MonoBehaviour
  11. {
  12.     private MySqlConnection connection;
  13.     private string server;
  14.     private string database;
  15.     private string uid;
  16.     private string password;
  17.  
  18.     public NewBehaviourScript()
  19.     {
  20.         Initialize();
  21.     }
  22.     private void Initialize()
  23.     {
  24.         server = "localhost";
  25.         database = "test";
  26.         uid = "root";
  27.         password = "root";
  28.         string connectionString;
  29.         connectionString = "SERVER=" + server + ";" + "DATABASE=" +
  30.         database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
  31.  
  32.         connection = new MySqlConnection(connectionString);
  33.     }
  34.     private bool OpenConnection()
  35.     {
  36.         try
  37.         {
  38.             connection.Open();
  39.             return true;
  40.  
  41.         }
  42.         catch (MySqlException ex)
  43.         {
  44.             //When handling errors, you can your application's response based
  45.             //on the error number.
  46.             //The two most common error numbers when connecting are as follows:
  47.             //0: Cannot connect to server.
  48.             //1045: Invalid user name and/or password.
  49.             switch (ex.Number)
  50.             {
  51.                 case 0:
  52.                     layout.chat = "0: Cannot connect to server.d";
  53.                     break;
  54.  
  55.                 case 1045:
  56.                     layout.chat = "1045: Invalid user name and/or password.";
  57.                     break;
  58.             }
  59.             return false;
  60.         }
  61.     }
  62.     private bool CloseConnection()
  63.     {
  64.         try
  65.         {
  66.             connection.Close();
  67.             return true;
  68.         }
  69.         catch (MySqlException ex)
  70.         {
  71.             return false;
  72.         }
  73.     }
  74.  
  75.     //Insert statement
  76.     public void Insert()
  77.     {
  78.         string query = "INSERT INTO tableinfo (name, age) VALUES('John Smith', '33')";
  79.  
  80.         //open connection
  81.         if (this.OpenConnection() == true)
  82.         {
  83.             //create command and assign the query and connection from the constructor
  84.             MySqlCommand cmd = new MySqlCommand(query, connection);
  85.  
  86.             //Execute command
  87.             cmd.ExecuteNonQuery();
  88.  
  89.             //close connection
  90.             this.CloseConnection();
  91.         }
  92.     }
  93.  
  94.     //Update statement
  95.     public void Update()
  96.     {
  97.         string query = "UPDATE tableinfo SET name='Joe', age='22' WHERE name='John Smith'";
  98.  
  99.         //Open connection
  100.         if (this.OpenConnection() == true)
  101.         {
  102.             //create mysql command
  103.             MySqlCommand cmd = new MySqlCommand();
  104.             //Assign the query using CommandText
  105.             cmd.CommandText = query;
  106.             //Assign the connection using Connection
  107.             cmd.Connection = connection;
  108.  
  109.             //Execute query
  110.             cmd.ExecuteNonQuery();
  111.  
  112.             //close connection
  113.             this.CloseConnection();
  114.         }
  115.     }
  116.  
  117.     //Delete statement
  118.     public void Delete()
  119.     {
  120.         string query = "DELETE FROM tableinfo WHERE name='John Smith'";
  121.  
  122.         if (this.OpenConnection() == true)
  123.         {
  124.             MySqlCommand cmd = new MySqlCommand(query, connection);
  125.             cmd.ExecuteNonQuery();
  126.             this.CloseConnection();
  127.         }
  128.     }
  129.  
  130.     //Select statement
  131.  
  132.     //Count statement
  133.     public int Count()
  134.     {
  135.         string query = "SELECT Count(*) FROM tableinfo";
  136.         int Count = -1;
  137.  
  138.         //Open Connection
  139.         if (this.OpenConnection() == true)
  140.         {
  141.             //Create Mysql Command
  142.             MySqlCommand cmd = new MySqlCommand(query, connection);
  143.  
  144.             //ExecuteScalar will return one value
  145.             Count = int.Parse(cmd.ExecuteScalar() + "");
  146.  
  147.             //close Connection
  148.             this.CloseConnection();
  149.  
  150.             return Count;
  151.         }
  152.         else
  153.         {
  154.             return Count;
  155.         }
  156.     }
  157.  
  158.     //Backup
  159.     public void Backup()
  160.     {
  161.     }
  162.  
  163.     //Restore
  164.     public void Restore()
  165.     {
  166.     }
  167.  
  168. }
  169.  
  170. //open connection to database
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement