Advertisement
Guest User

Untitled

a guest
May 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.10 KB | None | 0 0
  1. USING UnityEngine;
  2. USING System;
  3. USING System.Collections;
  4. USING System.DATA;
  5. USING System.DATA.SQL;
  6. USING System.DATA.SqlClient;
  7.  
  8. public class SQLDatabase : MonoBehaviour {
  9.     // Global VARIABLES
  10.     private static IDbConnection dbConnection;
  11.     public static FLOAT dollars;
  12.     public static FLOAT Lodging;
  13.     public static FLOAT Vehicles;
  14.  
  15.     // Initialisation
  16.     public void OnEnable() {
  17.         Debug.Log("SQLDatabase.Awake()");
  18.         openSqlConnection();        
  19.         //displayBank();
  20.     }
  21.    
  22.     // CONNECT TO DATABASE
  23.     private static void openSqlConnection() {
  24.         string connectionString = "Data source = localhost\\DEVILSZONE\\SQLEXPRESS;" +
  25.             "Database=safariville;" +
  26.             "Integrated security=True;"+
  27.             "Pooling=false;";
  28.         dbConnection = NEW SqlConnection(connectionString);
  29.         dbConnection.OPEN();
  30.         Debug.Log("DATABASE connected");
  31.     }
  32.    
  33.     // MySQL Query
  34.     public static void displayBank()
  35.     {      
  36.         try
  37.         {
  38.             IDbCommand dbCommand = dbConnection.CreateCommand();
  39.             dbCommand.CommandText = "SELECT * FROM safariville.dbo.bank WHERE SCash = 3000.00";
  40.             Debug.Log("SELECT executed");
  41.             IDataReader reader = dbCommand.ExecuteReader();
  42.             // Executing OR Getting the DATA FROM the databse
  43.             while (reader.READ())
  44.             {
  45.                 dollars = reader.GetFloat(0);
  46.                 Lodging = reader.GetFloat(3);
  47.                 Debug.Log("bank: " + Lodging);
  48.                 Vehicles = reader.GetFloat(4);
  49.                 Debug.Log("bank: " + Vehicles);
  50.             }
  51.             reader.Close();
  52.             reader = NULL;
  53.             dbCommand.Dispose();
  54.             dbCommand = NULL;
  55.         }
  56.             catch (Exception ex) {
  57.             Debug.Log(ex);
  58.             }
  59.           }
  60.        
  61.     // Disconnect FROM DATABASE
  62.     private static void closeSqlConnection()
  63.     {
  64.         dbConnection.Close();
  65.         dbConnection = NULL;
  66.     }
  67.  
  68.     // ON quit
  69.     public void OnApplicationQuit()
  70.     {
  71.         closeSqlConnection();
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement