Guest User

Untitled

a guest
Nov 13th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. using System;
  2.  
  3. // REQUIERE LA REFERENCIA System.Data
  4. using System.Data;
  5.  
  6. // REQUIERE LA REFERENCIA MySql.Data
  7. using MySql.Data.MySqlClient;
  8. using System.IO;
  9.  
  10. /*
  11. * CLASE MYSQLCS
  12. * Agregar al proyecto con using MysqlCS;
  13. * REQUIERE CAMBIAR LOS PARÁMETROS DE CONEXIÓN
  14. */
  15. namespace MysqlCS
  16. {
  17. public class Connection
  18. {
  19. protected string host = "127.0.0.1";
  20. protected string port = "3306";
  21. protected string db = "MI_BASE_DE_DATOS";
  22. protected string user = "MI_USUARIO";
  23. protected string pass = "MI_CONTRASEÑA";
  24.  
  25. protected MySqlConnection cnn;
  26. protected MySqlCommand cmd;
  27. protected string enlace;
  28. protected bool activo = false;
  29.  
  30. /*
  31. * CONSTRUCTOR
  32. */
  33. public Connection()
  34. {
  35. try
  36. {
  37. enlace =
  38. "Server=" + host +
  39. ";Port=" + port +
  40. ";User ID=" + user +
  41. ";Database=" + db +
  42. ";Password=" + pass +
  43. ";";
  44.  
  45. cnn = new MySqlConnection(enlace);
  46. cnn.Open();
  47. }
  48. catch (Exception e)
  49. {
  50. Console.WriteLine("ERROR: {0}", e.Message);
  51. Console.ReadKey();
  52.  
  53. // Cierra la aplicación en caso de ERROR
  54. Environment.Exit(1);
  55. }
  56. }
  57.  
  58. /*
  59. * COUNT ALL ROWS
  60. * Devuelve total de filas de una tabla
  61. * @param string
  62. * @return int
  63. */
  64. public int count_all_rows(string tabla)
  65. {
  66. int count = 0;
  67.  
  68. using(cmd = new MySqlCommand("SELECT COUNT(*) FROM " + tabla, cnn))
  69. {
  70. count = Convert.ToInt32(cmd.ExecuteScalar());
  71. cmd.Dispose();
  72. }
  73.  
  74. return count;
  75. }
  76.  
  77. /*
  78. * QUERY
  79. * Ejecuta una solicitud al servidor
  80. * @param string
  81. * @return void
  82. */
  83. public void query(string solicitud)
  84. {
  85. cmd = new MySqlCommand(solicitud, cnn);
  86. activo = true;
  87. }
  88.  
  89. /*
  90. * DATATABLE
  91. * Devuelve los datos de una solicitud de la función QUERY
  92. * Los datos retornados son del tipo DataTable (System.Data)
  93. * @param
  94. * @return DataTable
  95. */
  96. public DataTable datatable()
  97. {
  98. if (activo)
  99. {
  100. DataTable data = new DataTable();
  101. data.Load(cmd.ExecuteReader());
  102.  
  103. cmd.Dispose();
  104. activo = false;
  105.  
  106. return data;
  107. }
  108. else
  109. return new DataTable();
  110. }
  111.  
  112. /*
  113. * FIRST DATA
  114. * Devuelve la primera fila de una tabla después de una solicitud con QUERY
  115. * El resultado se almacena en un DataRow (System.Data)
  116. * @param
  117. * @return DataRow
  118. */
  119. public DataRow first_data()
  120. {
  121. if (activo)
  122. {
  123. DataTable data = datatable();
  124. activo = false;
  125.  
  126. if (data.Rows.Count > 0)
  127. return data.Rows [0];
  128. else
  129. return null;
  130. }
  131.  
  132. return null;
  133. }
  134.  
  135. /*
  136. * ONLY EXEC
  137. * Ejecuta una solicitud QUERY y devuelve el Identificador de INSERT
  138. * @param
  139. * @return int
  140. */
  141. public int only_exec()
  142. {
  143. int id = 0;
  144.  
  145. if (activo)
  146. {
  147. cmd.ExecuteNonQuery();
  148. id = (int)cmd.LastInsertedId;
  149. cmd.Dispose();
  150. }
  151.  
  152. activo = false;
  153. return id;
  154. }
  155.  
  156. /*
  157. * DESTRUCTOR
  158. * Libera la memoria y cierra la conexión
  159. * @param
  160. * @return
  161. */
  162. ~Connection()
  163. {
  164. cnn.Close();
  165. }
  166. }
  167. }
  168.  
  169. /* Licencia MIT - OOSJ */
Add Comment
Please, Sign In to add comment