PsyOps

SQL Test

Jan 11th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using BattleCore;
  7. using BattleCore.Events;
  8. using PsyModules;
  9. using System.IO;
  10.  
  11. using MySql.Data.MySqlClient;
  12. using System.Diagnostics;
  13.  
  14. namespace SQL_Test
  15. {
  16. // Add the attribute and the base class
  17. [Behavior("SQLTest1", "true", "0.1", "udp", "Testing misc stuff")]
  18. public class SQLTest1 : BotEventListener
  19. {
  20. private MySqlConnection connection;
  21. private string server;
  22. private string database;
  23. private string uid;
  24. private string password;
  25.  
  26. ShortChat msg = new ShortChat();
  27.  
  28. //Constructor
  29. public SQLTest1()
  30. {
  31. RegisterCommand("!doit", doEeeht);
  32. RegisterCommand("!connect", doConn);
  33.  
  34. }
  35.  
  36. public void doEeeht(ChatEvent c)
  37. {
  38. Initialize();
  39. }
  40. public void doConn(ChatEvent c)
  41. {
  42. if (OpenConnection())
  43. Game(msg.arena("Connected"));
  44. else
  45. Game(msg.arena("Phail"));
  46. }
  47. //Initialize values
  48. private void Initialize()
  49. {
  50. server = "localhost";
  51. database = "mydb";
  52. uid = "root";
  53. password = "XXXXXXXXXXXXX";
  54. string connectionString;
  55. connectionString = "SERVER=" + server + ";" + "DATABASE=" +
  56. database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
  57.  
  58. connection = new MySqlConnection(connectionString);
  59. }
  60.  
  61. //open connection to database
  62. private bool OpenConnection()
  63. {
  64. try
  65. {
  66. connection.Open();
  67. return true;
  68. }
  69. catch (MySqlException ex)
  70. {
  71. //When handling errors, you can your application's response based
  72. //on the error number.
  73. //The two most common error numbers when connecting are as follows:
  74. //0: Cannot connect to server.
  75. //1045: Invalid user name and/or password.
  76. switch (ex.Number)
  77. {
  78. case 0:
  79. Game(msg.arena("Cannot connect to server. Contact administrator"));
  80. break;
  81.  
  82. case 1045:
  83. Game(msg.arena("Invalid username/password, please try again"));
  84. break;
  85. }
  86. return false;
  87. }
  88. }
  89.  
  90. //Close connection
  91. private bool CloseConnection()
  92. {
  93. try
  94. {
  95. connection.Close();
  96. return true;
  97. }
  98. catch (MySqlException ex)
  99. {
  100. Game(msg.arena(ex.Message));
  101. return false;
  102. }
  103. }
  104.  
  105. //Insert statement
  106. public void Insert()
  107. {
  108. string query = "INSERT INTO tableinfo (name, age) VALUES('John Smith', '33')";
  109.  
  110. //open connection
  111. if (this.OpenConnection() == true)
  112. {
  113. //create command and assign the query and connection from the constructor
  114. MySqlCommand cmd = new MySqlCommand(query, connection);
  115.  
  116. //Execute command
  117. cmd.ExecuteNonQuery();
  118.  
  119. //close connection
  120. this.CloseConnection();
  121. }
  122. }
  123.  
  124. //Update statement
  125. public void Update()
  126. {
  127. string query = "UPDATE tableinfo SET name='Joe', age='22' WHERE name='John Smith'";
  128.  
  129. //Open connection
  130. if (this.OpenConnection() == true)
  131. {
  132. //create mysql command
  133. MySqlCommand cmd = new MySqlCommand();
  134. //Assign the query using CommandText
  135. cmd.CommandText = query;
  136. //Assign the connection using Connection
  137. cmd.Connection = connection;
  138.  
  139. //Execute query
  140. cmd.ExecuteNonQuery();
  141.  
  142. //close connection
  143. this.CloseConnection();
  144. }
  145. }
  146.  
  147.  
  148. //Delete statement
  149. public void Delete()
  150. {
  151. string query = "DELETE FROM tableinfo WHERE name='John Smith'";
  152.  
  153. if (this.OpenConnection() == true)
  154. {
  155. MySqlCommand cmd = new MySqlCommand(query, connection);
  156. cmd.ExecuteNonQuery();
  157. this.CloseConnection();
  158. }
  159. }
  160.  
  161. //Select statement
  162. public List<string>[] Select()
  163. {
  164. string query = "SELECT * FROM tableinfo";
  165.  
  166. //Create a list to store the result
  167. List<string>[] list = new List<string>[3];
  168. list[0] = new List<string>();
  169. list[1] = new List<string>();
  170. list[2] = new List<string>();
  171.  
  172. //Open connection
  173. if (this.OpenConnection() == true)
  174. {
  175. //Create Command
  176. MySqlCommand cmd = new MySqlCommand(query, connection);
  177. //Create a data reader and Execute the command
  178. MySqlDataReader dataReader = cmd.ExecuteReader();
  179.  
  180. //Read the data and store them in the list
  181. while (dataReader.Read())
  182. {
  183. list[0].Add(dataReader["id"] + "");
  184. list[1].Add(dataReader["name"] + "");
  185. list[2].Add(dataReader["age"] + "");
  186. }
  187.  
  188. //close Data Reader
  189. dataReader.Close();
  190.  
  191. //close Connection
  192. this.CloseConnection();
  193.  
  194. //return list to be displayed
  195. return list;
  196. }
  197. else
  198. {
  199. return list;
  200. }
  201. }
  202.  
  203. //Count statement
  204. public int Count()
  205. {
  206. string query = "SELECT Count(*) FROM tableinfo";
  207. int Count = -1;
  208.  
  209. //Open Connection
  210. if (this.OpenConnection() == true)
  211. {
  212. //Create Mysql Command
  213. MySqlCommand cmd = new MySqlCommand(query, connection);
  214.  
  215. //ExecuteScalar will return one value
  216. Count = int.Parse(cmd.ExecuteScalar() + "");
  217.  
  218. //close Connection
  219. this.CloseConnection();
  220.  
  221. return Count;
  222. }
  223. else
  224. {
  225. return Count;
  226. }
  227. }
  228.  
  229. //Backup
  230. public void Backup()
  231. {
  232. try
  233. {
  234. DateTime Time = DateTime.Now;
  235. int year = Time.Year;
  236. int month = Time.Month;
  237. int day = Time.Day;
  238. int hour = Time.Hour;
  239. int minute = Time.Minute;
  240. int second = Time.Second;
  241. int millisecond = Time.Millisecond;
  242.  
  243. //Save file to C:\ with the current date as a filename
  244. string path;
  245. path = "C:\\MySqlBackup" + year + "-" + month + "-" + day +
  246. "-" + hour + "-" + minute + "-" + second + "-" + millisecond + ".sql";
  247. StreamWriter file = new StreamWriter(path);
  248.  
  249.  
  250. ProcessStartInfo psi = new ProcessStartInfo();
  251. psi.FileName = "mysqldump";
  252. psi.RedirectStandardInput = false;
  253. psi.RedirectStandardOutput = true;
  254. psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}",
  255. uid, password, server, database);
  256. psi.UseShellExecute = false;
  257.  
  258. Process process = Process.Start(psi);
  259.  
  260. string output;
  261. output = process.StandardOutput.ReadToEnd();
  262. file.WriteLine(output);
  263. process.WaitForExit();
  264. file.Close();
  265. process.Close();
  266. }
  267. catch (IOException ex)
  268. {
  269. Game(msg.arena("Error , unable to backup!"));
  270. }
  271. }
  272. //Restore
  273. public void Restore()
  274. {
  275. try
  276. {
  277. //Read file from C:\
  278. string path;
  279. path = "C:\\MySqlBackup.sql";
  280. StreamReader file = new StreamReader(path);
  281. string input = file.ReadToEnd();
  282. file.Close();
  283.  
  284. ProcessStartInfo psi = new ProcessStartInfo();
  285. psi.FileName = "mysql";
  286. psi.RedirectStandardInput = true;
  287. psi.RedirectStandardOutput = false;
  288. psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}",
  289. uid, password, server, database);
  290. psi.UseShellExecute = false;
  291.  
  292.  
  293. Process process = Process.Start(psi);
  294. process.StandardInput.WriteLine(input);
  295. process.StandardInput.Close();
  296. process.WaitForExit();
  297. process.Close();
  298. }
  299. catch (IOException ex)
  300. {
  301. Game(msg.arena("Error , unable to Restore!"));
  302. }
  303. }
  304.  
  305. public override void Dispose()
  306. {
  307. throw new NotImplementedException();
  308. }
  309. }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment