SHARE
TWEET

MySQL for C#

capasha Aug 4th, 2015 (edited) 290 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. MySQL for C#
  3. Do whatever you want to the MySQL database. Use it as a test.
  4. Host: sql5.freemysqlhosting.net
  5. Database name: sql585909
  6. Database user: sql585909
  7. Database password: aN4!uX9*
  8. Port number: 3306
  9.  
  10. Download MySQL DLL: http://www.mediafire.com/download/2wqask3qnzs9jn5/MySql.Data.dll
  11. */
  12. using MySql.Data.MySqlClient;
  13. private Dictionary<string, string> banned = new Dictionary<string, string>(); //Read bans from the dictionary "banned".
  14. var server = "sql5.freemysqlhosting.net"; //Server
  15. var database = "sql585909"; //Database
  16. var password = "aN4!uX9*"; //Password
  17. var banTable = "Bans"; //Table
  18. var banColumn = "Name"; //Column
  19. var banColumn1 = "Reason"; //Column
  20. var conString = "Server=" + server ";Database=" + database + ";Uid=" + database + ";Pw=" + password + ";"; //Database reading
  21.         private void readbans()
  22.         {
  23.             banned.Clear();
  24.             using (var con = new MySqlConnection(conString))
  25.             {
  26.                 string query = "SELECT * from " + banTable + " ORDER BY " + banColumn + " DESC";
  27.  
  28.                 using (var cmd = new MySqlCommand(query, con))
  29.                 {
  30.                     con.Open();
  31.  
  32.                     var reader = cmd.ExecuteReader();
  33.                     while (reader.Read())
  34.                     {
  35.                         banned.Add(reader.GetString(banColumn), reader.GetString(banColumn1));
  36.  
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.         private void DelBan(string name,string reason)
  42.         {
  43.             try
  44.             {
  45.  
  46.                 using (var con = new MySqlConnection(conString))
  47.                 {
  48.  
  49.                     var query = "DELETE FROM `" + database + "`.`" banTable + "` WHERE `" banColumn + "` = '" + name + "' AND `" + banTable + "`.`" + banColumn1 + "` = '" + reason + "'";
  50.                     using (var cmd = new MySqlCommand(query, con))
  51.                     {
  52.                         con.Open();
  53.                         cmd.ExecuteNonQuery();
  54.                         con.Close();
  55.                     }
  56.                 }
  57.             }
  58.             catch (MySqlException ex)
  59.             {
  60.  
  61.             }
  62.         }
  63.         private void AddBans(string name, string reason)
  64.         {
  65.             try
  66.             {
  67.  
  68.                 using (var con = new MySqlConnection(conString))
  69.                 {
  70.  
  71.                     var query = "INSERT INTO `" + database + "`.`" + banTable + "`(`" + banColumn + "` ,`" + banColumn1 + "` ) VALUES('" + name + "','" + reason + "')";
  72.                     using (var cmd = new MySqlCommand(query, con))
  73.                     {
  74.                         con.Open();
  75.                         cmd.ExecuteNonQuery();
  76.                         con.Close();
  77.                     }
  78.                 }
  79.             }
  80.             catch (MySqlException ex)
  81.             {
  82.  
  83.             }
  84.         }
  85.         private void UpdateBans(string name, string reason)
  86.         {
  87.             try
  88.             {
  89.                 using (var con = new MySqlConnection(conString))
  90.                 {
  91.                     var query = "";
  92.  
  93.                     query = "UPDATE `" + database + "`.`" + banTable + "` SET `" + banColumn1 + "` = '" + reason + "' WHERE `" + banTable + "`.`" + banColumn + "` = '" + name + "'; ";
  94.                     using (var cmd = new MySqlCommand(query, con))
  95.                     {
  96.                         con.Open();
  97.                         cmd.ExecuteNonQuery();
  98.                         con.Close();
  99.                     }
  100.  
  101.                 }
  102.             }
  103.             catch (MySqlException ex)
  104.             {
  105.                 Console.WriteLine(ex.Message);
  106.             }
  107.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top