Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using MySql.Data.MySqlClient;
- using MySql;
- using MySql.Data;
- using MySql.Data.Types;
- using System.Diagnostics;
- using System.Data;
- public class NewBehaviourScript : MonoBehaviour
- {
- private MySqlConnection connection;
- private string server;
- private string database;
- private string uid;
- private string password;
- public NewBehaviourScript()
- {
- Initialize();
- }
- private void Initialize()
- {
- server = "localhost";
- database = "test";
- uid = "root";
- password = "root";
- string connectionString;
- connectionString = "SERVER=" + server + ";" + "DATABASE=" +
- database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
- connection = new MySqlConnection(connectionString);
- }
- private bool OpenConnection()
- {
- try
- {
- connection.Open();
- return true;
- }
- catch (MySqlException ex)
- {
- //When handling errors, you can your application's response based
- //on the error number.
- //The two most common error numbers when connecting are as follows:
- //0: Cannot connect to server.
- //1045: Invalid user name and/or password.
- switch (ex.Number)
- {
- case 0:
- layout.chat = "0: Cannot connect to server.d";
- break;
- case 1045:
- layout.chat = "1045: Invalid user name and/or password.";
- break;
- }
- return false;
- }
- }
- private bool CloseConnection()
- {
- try
- {
- connection.Close();
- return true;
- }
- catch (MySqlException ex)
- {
- return false;
- }
- }
- //Insert statement
- public void Insert()
- {
- string query = "INSERT INTO tableinfo (name, age) VALUES('John Smith', '33')";
- //open connection
- if (this.OpenConnection() == true)
- {
- //create command and assign the query and connection from the constructor
- MySqlCommand cmd = new MySqlCommand(query, connection);
- //Execute command
- cmd.ExecuteNonQuery();
- //close connection
- this.CloseConnection();
- }
- }
- //Update statement
- public void Update()
- {
- string query = "UPDATE tableinfo SET name='Joe', age='22' WHERE name='John Smith'";
- //Open connection
- if (this.OpenConnection() == true)
- {
- //create mysql command
- MySqlCommand cmd = new MySqlCommand();
- //Assign the query using CommandText
- cmd.CommandText = query;
- //Assign the connection using Connection
- cmd.Connection = connection;
- //Execute query
- cmd.ExecuteNonQuery();
- //close connection
- this.CloseConnection();
- }
- }
- //Delete statement
- public void Delete()
- {
- string query = "DELETE FROM tableinfo WHERE name='John Smith'";
- if (this.OpenConnection() == true)
- {
- MySqlCommand cmd = new MySqlCommand(query, connection);
- cmd.ExecuteNonQuery();
- this.CloseConnection();
- }
- }
- //Select statement
- //Count statement
- public int Count()
- {
- string query = "SELECT Count(*) FROM tableinfo";
- int Count = -1;
- //Open Connection
- if (this.OpenConnection() == true)
- {
- //Create Mysql Command
- MySqlCommand cmd = new MySqlCommand(query, connection);
- //ExecuteScalar will return one value
- Count = int.Parse(cmd.ExecuteScalar() + "");
- //close Connection
- this.CloseConnection();
- return Count;
- }
- else
- {
- return Count;
- }
- }
- //Backup
- public void Backup()
- {
- }
- //Restore
- public void Restore()
- {
- }
- }
- //open connection to database
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement