static FakeDb() { var assembly = Assembly.GetExecutingAssembly(); var stream = assembly.GetManifestResourceStream("DataLayer.words.txt"); var reader = new StreamReader(stream); _words = reader.ReadToEnd().Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 1; i <= 20; i++) { Category c = new Category(); c.Id = i; c.Name = _words[_rnd.Next(_words.Length)]; _categories.Add(c); for (int j = 1; j <= 20; j++) { Product p = new Product(); p.Id = j; p.CategoryId = i; p.Name = _words[_rnd.Next(_words.Length)]; _products.Add(p); } } }
public Category GetCategoryById(int categoryId) { using (var connection = new SqlConnection(_connectionString)) using (var cmd = connection.CreateCommand()) { cmd.CommandText = "SELECT * FROM Categories WHERE CategoryId = @categoryId"; connection.Open(); cmd.Parameters.AddWithValue("@categoryId", categoryId); var reader = cmd.ExecuteReader(); reader.Read(); Category c = new Category(); c.Id = (int) reader["CategoryId"]; c.Name = (string) reader["CategoryName"]; c.Description = (string) reader["Description"]; return c; } }
public IEnumerable<Category> GetAll() { List<Category> result = new List<Category>(); using (var connection = new SqlConnection(_connectionString)) using (var cmd = connection.CreateCommand()) { cmd.CommandText = "SELECT * FROM Categories"; connection.Open(); var reader = cmd.ExecuteReader(); while (reader.Read()) { Category c = new Category(); c.Id = (int) reader["CategoryId"]; c.Name = (string) reader["CategoryName"]; c.Description = (string) reader["Description"]; result.Add(c); } } return result; }