Advertisement
Brandan

Untitled

Nov 1st, 2014
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using Microsoft.VisualBasic;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using MySql.Data.MySqlClient;
  8.  
  9.  
  10. public class Mysql
  11. {
  12.     private const string mysql_sever = "HOST";
  13.     private const string mysql_database = "DATABASE";
  14.  
  15.     private const int mysql_port = 3306;
  16.     private const string mysql_username = "USERNMAE";
  17.  
  18.     private const string mysql_password = "PASSWORD";
  19.  
  20.     public MySqlConnection SqlConnection;
  21.     public object connect()
  22.     {
  23.         dynamic mysql_serverstring = "Server=" + mysql_sever + ";User Id=" + mysql_username + ";Password=" + mysql_password + ";Database=" + mysql_database;
  24.         SqlConnection = new MySqlConnection();
  25.  
  26.         SqlConnection.ConnectionString = mysql_serverstring;
  27.  
  28.         try {
  29.             if (SqlConnection.State == ConnectionState.Closed) {
  30.                 SqlConnection.Open();
  31.                 Console.WriteLine(DateAndTime.Now + ": Successfully Connected to Database!");
  32.                 return true;
  33.             } else {
  34.                 Console.WriteLine(DateAndTime.Now + ": Failed to Connected to Database!");
  35.                 SqlConnection.Close();
  36.                 return false;
  37.             }
  38.         } catch (Exception ex) {
  39.             Console.WriteLine(DateAndTime.Now + ": Failed to connect to database! - " + ex.Message);
  40.             return false;
  41.         }
  42.     }
  43.  
  44.     public object isconnected()
  45.     {
  46.         if (SqlConnection.State == ConnectionState.Closed == false) {
  47.             return true;
  48.         }
  49.         return false;
  50.     }
  51.  
  52.     public bool query(q)
  53.     {
  54.         try {
  55.             if (isconnected()) {
  56.                 MySqlCommand cmd = new MySqlCommand(q, SqlConnection);
  57.                 return cmd.ExecuteNonQuery();
  58.             } else {
  59.                 connect();
  60.                 return false;
  61.             }
  62.         } catch (Exception ex) {
  63.             Console.WriteLine(DateAndTime.Now + ": My Sql Error - " + q + " - " + ex.Message);
  64.             return false;
  65.         }
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement