PageRenderTime 29ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/CapaDatos/DArticulo.cs

https://gitlab.com/JPGITLAB1519/Sistema-Ventas-C-
C# | 403 lines | 270 code | 101 blank | 32 comment | 9 complexity | 2217dd9aa1ee445f9f82a306236f8ec7 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. // para trabajar con los datos
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. namespace CapaDatos
  9. {
  10. public class DArticulo
  11. {
  12. private int _Idarticulo;
  13. private string _Codigo;
  14. private string _Nombre;
  15. private string _Descripcion;
  16. private byte[] _Imagen;
  17. private int _Idcategoria;
  18. private int _Idpresentacion;
  19. private string _TextoBuscar;
  20. public int Idarticulo
  21. {
  22. get { return _Idarticulo; }
  23. set { _Idarticulo = value; }
  24. }
  25. public string Nombre
  26. {
  27. get { return _Nombre; }
  28. set { _Nombre = value; }
  29. }
  30. public string Codigo
  31. {
  32. get { return _Codigo; }
  33. set { _Codigo = value; }
  34. }
  35. public string Descripcion
  36. {
  37. get { return _Descripcion; }
  38. set { _Descripcion = value; }
  39. }
  40. public byte[] Imagen
  41. {
  42. get { return _Imagen; }
  43. set { _Imagen = value; }
  44. }
  45. public int Idcategoria
  46. {
  47. get { return _Idcategoria; }
  48. set { _Idcategoria = value; }
  49. }
  50. public int Idpresentacion
  51. {
  52. get { return _Idpresentacion; }
  53. set { _Idpresentacion = value; }
  54. }
  55. public string TextoBuscar
  56. {
  57. get { return _TextoBuscar; }
  58. set { _TextoBuscar = value; }
  59. }
  60. // constructor vacio
  61. public DArticulo()
  62. {
  63. }
  64. // constructor con todos las propiedades
  65. public DArticulo(int idarticulo, string codigo, string nombre, string descripcion, byte[] imagen, int idcategoria, int idpresentacion )
  66. {
  67. this.Idarticulo = idarticulo;
  68. this.Codigo = codigo;
  69. this.Nombre = nombre;
  70. this.Descripcion = descripcion;
  71. this.Imagen = imagen;
  72. this.Idcategoria = idcategoria;
  73. this.Idpresentacion = idpresentacion;
  74. }
  75. // metodos
  76. public string Insertar(DArticulo Articulo)
  77. {
  78. string rpta = "";
  79. SqlConnection SqlCon = new SqlConnection();
  80. try
  81. {
  82. // conexion
  83. SqlCon.ConnectionString = Conexion.Cn;
  84. SqlCon.Open();
  85. // comandos
  86. // agregamos el nombre del metodo y sus parametros
  87. SqlCommand SqlCmd = new SqlCommand();
  88. SqlCmd.Connection = SqlCon;
  89. SqlCmd.CommandText = "spinsertar_articulo";
  90. SqlCmd.CommandType = CommandType.StoredProcedure;
  91. // parametros
  92. SqlParameter ParIdArticulo = new SqlParameter();
  93. ParIdArticulo.ParameterName = "@idarticulo";
  94. ParIdArticulo.SqlDbType = SqlDbType.Int;
  95. ParIdArticulo.Direction = ParameterDirection.Output;
  96. SqlCmd.Parameters.Add(ParIdArticulo);
  97. SqlParameter ParCodigo = new SqlParameter();
  98. ParCodigo.ParameterName = "@codigo";
  99. ParCodigo.SqlDbType = SqlDbType.VarChar;
  100. ParCodigo.Size = 50;
  101. ParCodigo.Value = Articulo.Codigo;
  102. SqlCmd.Parameters.Add(ParCodigo);
  103. SqlParameter ParNombre = new SqlParameter();
  104. ParNombre.ParameterName = "@nombre";
  105. ParNombre.SqlDbType = SqlDbType.VarChar;
  106. ParNombre.Size = 50;
  107. ParNombre.Value = Articulo.Nombre;
  108. SqlCmd.Parameters.Add(ParNombre);
  109. SqlParameter ParDescripcion = new SqlParameter();
  110. ParDescripcion.ParameterName = "@descripcion";
  111. ParDescripcion.SqlDbType = SqlDbType.VarChar;
  112. ParDescripcion.Size = 1024;
  113. ParDescripcion.Value = Articulo.Descripcion;
  114. SqlCmd.Parameters.Add(ParDescripcion);
  115. SqlParameter ParImagen = new SqlParameter();
  116. ParImagen.ParameterName = "@imagen";
  117. ParImagen.SqlDbType = SqlDbType.Image;
  118. ParImagen.Value = Articulo.Imagen;
  119. SqlCmd.Parameters.Add(ParImagen);
  120. SqlParameter ParIdCategoria = new SqlParameter();
  121. ParIdCategoria.ParameterName = "@idcategoria";
  122. ParIdCategoria.SqlDbType = SqlDbType.Int;
  123. ParIdCategoria.Value = Articulo.Idcategoria;
  124. SqlCmd.Parameters.Add(ParIdCategoria);
  125. SqlParameter ParIdPresentacion = new SqlParameter();
  126. ParIdPresentacion.ParameterName = "@idpresentacion";
  127. ParIdPresentacion.SqlDbType = SqlDbType.Int;
  128. ParIdPresentacion.Value = Articulo.Idpresentacion;
  129. SqlCmd.Parameters.Add(ParIdPresentacion);
  130. // ejecutar comando
  131. // si se inserto valor -> ok else no se ingreso
  132. rpta = SqlCmd.ExecuteNonQuery() == 1 ? "OK" : "No se Actualizo el registro";
  133. }
  134. catch (Exception ex)
  135. {
  136. // en caso de error asignar a la variable el error devuelto
  137. rpta = ex.Message;
  138. }
  139. finally
  140. {
  141. // si la conexion esta abierta, cierrala
  142. if (SqlCon.State == ConnectionState.Open) SqlCon.Close();
  143. }
  144. return rpta;
  145. }
  146. public string Editar(DArticulo Articulo)
  147. {
  148. string rpta = "";
  149. SqlConnection SqlCon = new SqlConnection();
  150. try
  151. {
  152. // conexion
  153. SqlCon.ConnectionString = Conexion.Cn;
  154. SqlCon.Open();
  155. // comandos
  156. // agregamos el nombre del metodo y sus parametros
  157. SqlCommand SqlCmd = new SqlCommand();
  158. SqlCmd.Connection = SqlCon;
  159. SqlCmd.CommandText = "speditar_articulo";
  160. SqlCmd.CommandType = CommandType.StoredProcedure;
  161. // parametros
  162. SqlParameter ParIdArticulo = new SqlParameter();
  163. ParIdArticulo.ParameterName = "@idarticulo";
  164. ParIdArticulo.SqlDbType = SqlDbType.Int;
  165. SqlCmd.Parameters.Add(ParIdArticulo);
  166. SqlParameter ParCodigo = new SqlParameter();
  167. ParCodigo.ParameterName = "@codigo";
  168. ParCodigo.SqlDbType = SqlDbType.VarChar;
  169. ParCodigo.Size = 50;
  170. ParCodigo.Value = Articulo.Codigo;
  171. SqlCmd.Parameters.Add(ParCodigo);
  172. SqlParameter ParNombre = new SqlParameter();
  173. ParNombre.ParameterName = "@nombre";
  174. ParNombre.SqlDbType = SqlDbType.VarChar;
  175. ParNombre.Size = 50;
  176. ParNombre.Value = Articulo.Nombre;
  177. SqlCmd.Parameters.Add(ParNombre);
  178. SqlParameter ParDescripcion = new SqlParameter();
  179. ParDescripcion.ParameterName = "@descripcion";
  180. ParDescripcion.SqlDbType = SqlDbType.VarChar;
  181. ParDescripcion.Size = 1024;
  182. ParDescripcion.Value = Articulo.Descripcion;
  183. SqlCmd.Parameters.Add(ParDescripcion);
  184. SqlParameter ParImagen = new SqlParameter();
  185. ParImagen.ParameterName = "@imagen";
  186. ParImagen.SqlDbType = SqlDbType.Image;
  187. ParImagen.Value = Articulo.Imagen;
  188. SqlCmd.Parameters.Add(ParImagen);
  189. SqlParameter ParIdCategoria = new SqlParameter();
  190. ParIdCategoria.ParameterName = "@idcategoria";
  191. ParIdCategoria.SqlDbType = SqlDbType.Int;
  192. ParIdCategoria.Value = Articulo.Idcategoria;
  193. SqlCmd.Parameters.Add(ParIdCategoria);
  194. SqlParameter ParIdPresentacion = new SqlParameter();
  195. ParIdPresentacion.ParameterName = "@idpresentacion";
  196. ParIdPresentacion.SqlDbType = SqlDbType.Int;
  197. ParIdPresentacion.Value = Articulo.Idpresentacion;
  198. SqlCmd.Parameters.Add(ParIdPresentacion);
  199. // ejecutar comando
  200. // si se inserto valor -> ok else no se ingreso
  201. rpta = SqlCmd.ExecuteNonQuery() == 1 ? "OK" : "No se Actualizo el registro";
  202. }
  203. catch (Exception ex)
  204. {
  205. // en caso de error asignar a la variable el error devuelto
  206. rpta = ex.Message;
  207. }
  208. finally
  209. {
  210. // si la conexion esta abierta, cierrala
  211. if (SqlCon.State == ConnectionState.Open) SqlCon.Close();
  212. }
  213. return rpta;
  214. }
  215. public string Eliminar(DArticulo Articulo)
  216. {
  217. string rpta = "";
  218. SqlConnection SqlCon = new SqlConnection();
  219. try
  220. {
  221. // conexion
  222. SqlCon.ConnectionString = Conexion.Cn;
  223. SqlCon.Open();
  224. // comandos
  225. // agregamos el nombre del metodo y sus parametros
  226. SqlCommand SqlCmd = new SqlCommand();
  227. SqlCmd.Connection = SqlCon;
  228. SqlCmd.CommandText = "speliminar_articulo";
  229. SqlCmd.CommandType = CommandType.StoredProcedure;
  230. // parametros
  231. SqlParameter ParIdArticulo = new SqlParameter();
  232. ParIdArticulo.ParameterName = "@idarticulo";
  233. ParIdArticulo.SqlDbType = SqlDbType.Int;
  234. SqlCmd.Parameters.Add(ParIdArticulo);
  235. // ejecutar comando
  236. // si se inserto valor -> ok else no se ingreso
  237. rpta = SqlCmd.ExecuteNonQuery() == 1 ? "OK" : "No se Actualizo el registro";
  238. }
  239. catch (Exception ex)
  240. {
  241. // en caso de error asignar a la variable el error devuelto
  242. rpta = ex.Message;
  243. }
  244. finally
  245. {
  246. // si la conexion esta abierta, cierrala
  247. if (SqlCon.State == ConnectionState.Open) SqlCon.Close();
  248. }
  249. return rpta;
  250. }
  251. // metodo mostrar
  252. public DataTable Mostrar()
  253. {
  254. DataTable DtResultado = new DataTable("presentacion");
  255. SqlConnection SqlCon = new SqlConnection();
  256. try
  257. {
  258. SqlCon.ConnectionString = Conexion.Cn;
  259. SqlCommand SqlCmd = new SqlCommand();
  260. SqlCmd.Connection = SqlCon;
  261. SqlCmd.CommandText = "spmostrar_presentacion";
  262. SqlCmd.CommandType = CommandType.StoredProcedure;
  263. // ejecutar comando y llenar el datatable
  264. SqlDataAdapter SqlDat = new SqlDataAdapter(SqlCmd);
  265. SqlDat.Fill(DtResultado);
  266. }
  267. catch (Exception ex)
  268. {
  269. DtResultado = null;
  270. }
  271. return DtResultado;
  272. }
  273. // metodo buscar por nombre
  274. public DataTable BuscarNombre(DArticulo Articulo)
  275. {
  276. DataTable DtResultado = new DataTable("presentacion");
  277. SqlConnection SqlCon = new SqlConnection();
  278. try
  279. {
  280. SqlCon.ConnectionString = Conexion.Cn;
  281. SqlCommand SqlCmd = new SqlCommand();
  282. SqlCmd.Connection = SqlCon;
  283. SqlCmd.CommandText = "spbuscar_presentacion_nombre";
  284. SqlCmd.CommandType = CommandType.StoredProcedure;
  285. SqlParameter ParTextoBuscar = new SqlParameter();
  286. ParTextoBuscar.ParameterName = "@textobuscar";
  287. ParTextoBuscar.SqlDbType = SqlDbType.VarChar;
  288. ParTextoBuscar.Size = 50;
  289. ParTextoBuscar.Value = Articulo.TextoBuscar;
  290. SqlCmd.Parameters.Add(ParTextoBuscar);
  291. // ejecutar comando y llenar el datatable
  292. SqlDataAdapter SqlDat = new SqlDataAdapter(SqlCmd);
  293. SqlDat.Fill(DtResultado);
  294. }
  295. catch (Exception ex)
  296. {
  297. DtResultado = null;
  298. }
  299. return DtResultado;
  300. }
  301. }
  302. }