Advertisement
Guest User

Untitled

a guest
Nov 25th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MySql.Data.MySqlClient;
  6. using System.Data;
  7.  
  8. namespace MySQLClass
  9. {
  10.  
  11. //Don't forget to add the MySQL.Data dll to your projects references
  12. //It can be downloaded for free from MySQL's official website.
  13. //Link to the .NET Connector (MS Installer) http://dev.mysql.com/downloads/connector/net/
  14.  
  15.  
  16. class MySQLClient
  17. {
  18. MySqlConnection conn = null;
  19.  
  20.  
  21. #region Constructors
  22. public MySQLClient(string hostname, string database, string username, string password)
  23. {
  24. conn = new MySqlConnection("host=" + hostname + ";database=" + database +";username=" + username +";password=" + password +";");
  25. }
  26.  
  27. public MySQLClient(string hostname, string database, string username, string password, int portNumber)
  28. {
  29. conn = new MySqlConnection("host=" + hostname + ";database=" + database + ";username=" + username + ";password=" + password + ";port=" + portNumber.ToString() +";");
  30. }
  31.  
  32. public MySQLClient(string hostname, string database, string username, string password, int portNumber, int connectionTimeout)
  33. {
  34. conn = new MySqlConnection("host=" + hostname + ";database=" + database + ";username=" + username + ";password=" + password + ";port=" + portNumber.ToString() + ";Connection Timeout=" + connectionTimeout.ToString() +";");
  35. }
  36. #endregion
  37.  
  38. #region Open/Close Connection
  39. private bool Open()
  40. {
  41. //This opens temporary connection
  42. try
  43. {
  44. conn.Open();
  45. return true;
  46. }
  47. catch
  48. {
  49. //Here you could add a message box or something like that so you know if there were an error.
  50. return false;
  51. }
  52. }
  53.  
  54. private bool Close()
  55. {
  56. //This method closes the open connection
  57. try
  58. {
  59. conn.Close();
  60. return true;
  61. }
  62. catch
  63. {
  64. return false;
  65. }
  66. }
  67. #endregion
  68.  
  69. public void Insert(string table, string column, string value)
  70. {
  71. //Insert values into the database.
  72.  
  73. //Example: INSERT INTO names (name, age) VALUES('John Smith', '33')
  74. //Code: MySQLClient.Insert("names", "name, age", "'John Smith, '33'");
  75. string query = "INSERT INTO " + table + " (" + column + ") VALUES (" + value + ")";
  76.  
  77. try
  78. {
  79. if (this.Open())
  80. {
  81. //Opens a connection, if succefull; run the query and then close the connection.
  82.  
  83. MySqlCommand cmd = new MySqlCommand(query, conn);
  84.  
  85. cmd.ExecuteNonQuery();
  86. this.Close();
  87. }
  88. }
  89. catch { }
  90. return;
  91. }
  92.  
  93. public void Update(string table, string SET, string WHERE)
  94. {
  95. //Update existing values in the database.
  96.  
  97. //Example: UPDATE names SET name='Joe', age='22' WHERE name='John Smith'
  98. //Code: MySQLClient.Update("names", "name='Joe', age='22'", "name='John Smith'");
  99. string query = "UPDATE " + table + " SET " + SET + " WHERE " + WHERE + "";
  100.  
  101. if (this.Open())
  102. {
  103. try
  104. {
  105. //Opens a connection, if succefull; run the query and then close the connection.
  106.  
  107. MySqlCommand cmd = new MySqlCommand(query, conn);
  108. cmd.ExecuteNonQuery();
  109. this.Close();
  110. }
  111. catch { this.Close(); }
  112. }
  113. return;
  114. }
  115.  
  116. public void Delete(string table, string WHERE)
  117. {
  118. //Removes an entry from the database.
  119.  
  120. //Example: DELETE FROM names WHERE name='John Smith'
  121. //Code: MySQLClient.Delete("names", "name='John Smith'");
  122. string query = "DELETE FROM " + table + " WHERE " + WHERE + "";
  123.  
  124. if (this.Open())
  125. {
  126. try
  127. {
  128. //Opens a connection, if succefull; run the query and then close the connection.
  129.  
  130. MySqlCommand cmd = new MySqlCommand(query, conn);
  131. cmd.ExecuteNonQuery();
  132. this.Close();
  133. }
  134. catch { this.Close(); }
  135. }
  136. return;
  137. }
  138.  
  139. public Dictionary<string, string> Select(string table, string WHERE)
  140. {
  141. //This methods selects from the database, it retrieves data from it.
  142. //You must make a dictionary to use this since it both saves the column
  143. //and the value. i.e. "age" and "33" so you can easily search for values.
  144.  
  145. //Example: SELECT * FROM names WHERE name='John Smith'
  146. // This example would retrieve all data about the entry with the name "John Smith"
  147.  
  148. //Code = Dictionary<string, string> myDictionary = Select("names", "name='John Smith'");
  149. //This code creates a dictionary and fills it with info from the database.
  150.  
  151. string query = "SELECT * FROM " + table + " WHERE " + WHERE + "";
  152.  
  153. Dictionary<string, string> selectResult = new Dictionary<string, string>();
  154.  
  155. if (this.Open())
  156. {
  157. MySqlCommand cmd = new MySqlCommand(query, conn);
  158. MySqlDataReader dataReader = cmd.ExecuteReader();
  159.  
  160. try
  161. {
  162. while (dataReader.Read())
  163. {
  164.  
  165. for (int i = 0; i < dataReader.FieldCount; i++)
  166. {
  167. selectResult.Add(dataReader.GetName(i).ToString(), dataReader.GetValue(i).ToString());
  168. }
  169.  
  170. }
  171. dataReader.Close();
  172. }
  173. catch { }
  174. this.Close();
  175.  
  176. return selectResult;
  177. }
  178. else
  179. {
  180. return selectResult;
  181. }
  182. }
  183.  
  184. public int Count(string table)
  185. {
  186. //This counts the numbers of entries in a table and returns it as an integear
  187.  
  188. //Example: SELECT Count(*) FROM names
  189. //Code: int myInt = MySQLClient.Count("names");
  190.  
  191. string query = "SELECT Count(*) FROM " + table + "";
  192. int Count = -1;
  193. if (this.Open() == true)
  194. {
  195. try
  196. {
  197. MySqlCommand cmd = new MySqlCommand(query, conn);
  198. Count = int.Parse(cmd.ExecuteScalar() + "");
  199. this.Close();
  200. }
  201. catch { this.Close(); }
  202. return Count;
  203. }
  204. else
  205. {
  206. return Count;
  207. }
  208. }
  209.  
  210. }
  211. }
  212.  
  213. dataGridView1=Mysql.query("Select * from tabela");
  214.  
  215. public DataTable ExecuteDataTable(string Query)
  216. {
  217. MySqlCommand cmd = new MySqlCommand(Query, conn);
  218. MySqlDataAdapter da = new MySqlDataAdapter();
  219. DataTable dt = new DataTable();
  220. try
  221. {
  222. cmd.CommandType = CommandType.Text;
  223. da.SelectCommand = cmd;
  224. da.Fill(dt);
  225. return dt;
  226. }
  227. catch (Exception ex)
  228. {
  229. throw new Exception(ex.Message);
  230. }
  231. finally
  232. {
  233. cmd.Connection.Close();
  234. cmd.Dispose();
  235. }
  236. }
  237.  
  238. dataGridView1.DataSource = Mysql.ExecuteDataTable("Select * from tabela"); //Popular o grid
  239. dataGridView1.DataBind(); //explodir na tela
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement