public static ClassProducts SelectProduct(int id) { ClassProducts result = null; DataTable dataTable = new DataTable("Products"); using (cmd = Conn.CreateCommand()) { conn.Open(); try { cmd.CommandText = "SELECT IdProduct, ImageProduct, NameProduct, DescriptionProduct, Price FROM Products WHERE [Products].[IdProduct]=? "; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("[IdProduct]", id); dataAdapter = new OdbcDataAdapter(cmd); dataAdapter.Fill(dataTable); dataAdapter.Dispose(); if (dataTable.Rows.Count > 0) { result = ClassProducts.Map(dataTable.Rows[0]); } } catch { throw; } finally { conn.Close(); } } return(result); }
public static List <ClassProducts> SelectProducts() { List <ClassProducts> result = new List <ClassProducts>(); DataTable dataTable = new DataTable(); using (cmd = Conn.CreateCommand()) { Conn.Open(); try { cmd.CommandText = "SELECT IdProduct, ImageProduct, NameProduct, DescriptionProduct, Price FROM Products WHERE(DescriptionProduct IS NOT NULL) ORDER BY NameProduct"; dataAdapter = new OdbcDataAdapter(cmd); dataAdapter.Fill(dataTable); dataAdapter.Dispose(); } catch { throw; } finally { conn.Close(); } int i = 1;//счетчик строк в таблице foreach (DataRow row in dataTable.Rows) { result.Add(ClassProducts.Map(row, i)); i++; } } return(result); }