using System.Collections.Generic;
using System.Data.SqlClient;
public int ProductId { get; set; }
public string ProductName { get; set; }
public decimal UnitPrice { get; set; }
public List<Product> GetProducts(string connectionString)
List<Product> products = new List<Product>();
using (SqlConnection connection = new SqlConnection(connectionString))
string query = "SELECT ProductId, ProductName, UnitPrice FROM Products";
using (SqlCommand command = new SqlCommand(query, connection))
using (SqlDataReader reader = command.ExecuteReader())
Product product = new Product
ProductId = reader.GetInt32(0),
ProductName = reader.GetString(1),
UnitPrice = reader.GetDecimal(2)
Console.WriteLine($"An SQL Exception occurred: {ex.Message}");
return new List<Product>();
Console.WriteLine($"An Exception occurred: {ex.Message}");
return new List<Product>();
public static void Main()
Console.WriteLine("-------------- STARTING ---------------");
string connectionString = "ValidConnectionString";
var mockConnection = new Mock<IDbConnection>();
var mockCommand = new Mock<IDbCommand>();
var mockReader = new Mock<IDataReader>();
mockReader.SetupSequence(r => r.Read())
mockReader.SetupSequence(r => r.GetInt32(0))
mockReader.SetupSequence(r => r.GetString(1))
mockReader.SetupSequence(r => r.GetDecimal(2))
var productData = new ProductData();
using (var con = mockConnection.Object)
var field = typeof(ProductData).GetField("ConnectionFactory", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var method = typeof(ProductData).GetMethod("GetProducts", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
var result = (List<Product>)method.Invoke(productData, new object[] { connectionString });
Console.WriteLine("result.Count: {result.Count}");
Console.WriteLine("Exception: {ex.Message}");