Guest User

Untitled

a guest
May 7th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8.  
  9. namespace CRUD {
  10. class SQL {
  11.  
  12. public void delete(string table, string where) {
  13. MySqlConnection con = connection();
  14. string sQuery = "DELETE FROM " + table + " WHERE " + where;
  15. using (MySqlCommand query = new MySqlCommand(sQuery, con)) {
  16. con.Open();
  17. int result = query.ExecuteNonQuery();
  18. if (result < 0) {
  19. Console.WriteLine("ERRO!");
  20. } else {
  21. Console.WriteLine("OK!");
  22. }
  23. con.Close();
  24. }
  25. }
  26.  
  27. public void update(string table, string[] columns, string[] values, string where) {
  28. MySqlConnection con = connection();
  29. string aux = "";
  30. string sQuery = "";
  31. for (int i = 0; i < columns.Length; i++) {
  32. if (i < (columns.Length - 1)) {
  33. aux += columns[i] + "='" + values[i] + "',";
  34. } else {
  35. aux += columns[i] + "='" + values[i] + "'";
  36. }
  37. }
  38.  
  39. sQuery = "UPDATE " + table + " SET " + aux + " WHERE " + where;
  40. using (MySqlCommand query = new MySqlCommand(sQuery, con)) {
  41. con.Open();
  42. int result = query.ExecuteNonQuery();
  43. if (result < 0) {
  44. Console.WriteLine("ERRO!");
  45. } else {
  46. Console.WriteLine("OK!");
  47. }
  48. con.Close();
  49. }
  50.  
  51. }
  52.  
  53. public void insert(string table, string[] columns, string[] values) {
  54. MySqlConnection con = connection();
  55. string sQuery = "";
  56. string auxColumns = "";
  57. string auxValues = "";
  58.  
  59.  
  60. for (int i = 0; i < columns.Length; i++) {
  61. if (i < (columns.Length - 1)) {
  62. auxColumns += "'" + columns[i] + "',";
  63. } else {
  64. auxColumns += columns[i];
  65. }
  66. }
  67. for (int i = 0; i < values.Length; i++) {
  68. if (i < (values.Length - 1)) {
  69. if (values[i].Contains("int*") == true) {
  70. auxValues += "" + values[i] + ",";
  71. } else {
  72. auxValues += "'" + values[i] + ",";
  73. }
  74. } else {
  75. auxValues += "'" + values[i] + "'";
  76. }
  77. }
  78.  
  79. sQuery = "INSERT INTO " + table + " (" + auxColumns + ") VALUES (" + auxValues + ")";
  80. using (MySqlCommand query = new MySqlCommand(sQuery, con)) {
  81. con.Open();
  82. int result = query.ExecuteNonQuery();
  83. if (result < 0) {
  84. Console.WriteLine("ERRO!");
  85. } else {
  86. Console.WriteLine("SUCESSO!");
  87. }
  88. con.Close();
  89. }
  90.  
  91. }
  92.  
  93. public List<string> Select(string table, string[] columns, string where) {
  94. MySqlConnection con = connection();
  95. List<string> results = new List<string>();
  96. string sQuery = "";
  97. string auxColumns = "";
  98.  
  99. if (columns[0] == "*") {
  100. auxColumns = "*";
  101. } else {
  102. for (int i = 0; i < columns.Length; i++) {
  103. if (i < (columns.Length - 1)) {
  104. auxColumns += columns[i] + ",";
  105. } else {
  106. auxColumns += columns[i];
  107. }
  108. }
  109. }
  110.  
  111. if (where != "") {
  112. sQuery = "SELECT " + auxColumns + " FROM " + table + " WHERE " + where;
  113. } else {
  114. sQuery = "SELECT " + auxColumns + " FROM " + table;
  115. }
  116. con.Open();
  117. using (MySqlCommand query = new MySqlCommand(sQuery, con)) {
  118. using (MySqlDataReader reader = query.ExecuteReader()) {
  119. int x = 0;
  120. while (reader.Read()) {
  121. string aux = "";
  122. for (int i = 0; i < reader.FieldCount; i++) {
  123. if (i == (reader.FieldCount - 1)) {
  124. aux += Convert.ToString(reader.GetValue(i));
  125. } else {
  126. aux += Convert.ToString(reader.GetValue(i)) + '|';
  127. }
  128. }
  129. results.Add(aux);;
  130. x++;
  131. }
  132. con.Close();
  133. }
  134. return results;
  135. }
  136. }
  137.  
  138. public int intSelect(string table, string[] columns, string where) {
  139. MySqlConnection con = connection();
  140. int x = 0;
  141. string auxColumns = "";
  142.  
  143. if (columns[0] == "*") {
  144. auxColumns = "*";
  145. } else {
  146. for (int i = 0; i < columns.Length; i++) {
  147. if (i < (columns.Length - 1)) {
  148. auxColumns += columns[i] + ",";
  149. } else {
  150. auxColumns += columns[i];
  151. }
  152. }
  153. }
  154.  
  155. string sQuery = "SELECT " + auxColumns + " FROM " + table + " WHERE " + where;
  156. MessageBox.Show(sQuery);
  157. con.Open();
  158. using (MySqlCommand query = new MySqlCommand(sQuery, con)) {
  159. using (MySqlDataReader reader = query.ExecuteReader()) {
  160. while (reader.Read()) {
  161. x++;
  162. }
  163. con.Close();
  164. return x;
  165. }
  166. }
  167.  
  168.  
  169. }
  170.  
  171. public MySqlConnection connection() {
  172. string host = "localhost";
  173. string user = "root";
  174. string pass = "";
  175. string database = "maco";
  176.  
  177. string sCon = "host='" + host + "';user='" + user + "';password='" + pass + "';database='" + database + "'";
  178. MySqlConnection con = new MySqlConnection(sCon);
  179. return con;
  180. }
  181. }
  182. }
Add Comment
Please, Sign In to add comment