public static ProductList GetProducts(string connectionString) { const string GetProductsQuery = "select ProductID, ProductName, QuantityPerUnit," + " UnitPrice, UnitsInStock " + " from Products where Discontinued = 0"; var products = new ProductList(); try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); if (conn.State == System.Data.ConnectionState.Open) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = GetProductsQuery; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { var product = new Product(); product.ProductID = reader.GetInt32(0); product.ProductName = reader.GetString(1); product.QuantityPerUnit = reader.GetString(2); product.UnitPrice = reader.GetDecimal(3); product.UnitsInStock = reader.GetInt16(4); products.Add(product); } } } } } return(products); } catch (Exception eSql) { Debug.WriteLine("Exception: " + eSql.Message); } return(null); }
public bool LoadFromJsonString(string json) { JsonArray result = JsonArray.Parse(json); ProductList products = new ProductList(); foreach (var jsonProd in result) { JsonObject jsonProdObject = jsonProd.GetObject(); products.Add(new Product { ProductID = Convert.ToInt32(jsonProdObject.GetNamedNumber("ProductID")), ProductName = jsonProdObject.GetNamedString("ProductName"), UnitPrice = (decimal)jsonProdObject.GetNamedNumber("UnitPrice"), QuantityPerUnit = jsonProdObject.GetNamedString("QuantityPerUnit"), UnitsInStock = Convert.ToInt16(jsonProdObject.GetNamedNumber("UnitsInStock")) }); } foreach (Product p in products) { this.Items.Add(p); } return(Items.Count > 0); }