Private static string _connectionString =>
@"Data Source=.\SQLEXPRESS;Initial Catalog=NorthWind2020;Integrated Security=True";
public static List<string> GetRandomProducts(int recordCount = 3)
{
List<string> productList = new();
return productList;
}
using System.Data.SqlClient;
public static List<string> GetRandomProducts(int recordCount = 3)
{
List<string> productList = new();
using (var cn = new SqlConnection() { ConnectionString = _connectionString })
{
}
return productList;
}
public static List<string> GetRandomProducts(int recordCount = 3)
{
List<string> productList = new();
using (var cn = new SqlConnection() { ConnectionString = _connectionString })
{
using (var cmd = new SqlCommand() { Connection = cn })
{
}
}
return productList;
}
public static List<string> GetRandomProducts(int recordCount = 3)
{
List<string> productList = new();
using (var cn = new SqlConnection() { ConnectionString = _connectionString })
{
using (var cmd = new SqlCommand() { Connection = cn })
{
// for your code use one line, no string concatenation
cmd.CommandText = $"SELECT TOP ({recordCount}) ProductName " +
"FROM (SELECT TOP (10) ProductName FROM dbo.Products) a " +
"ORDER BY NEWID();";
}
}
return productList;
}
public static List<string> GetRandomProducts(int recordCount = 3)
{
List<string> productList = new();
using (var cn = new SqlConnection() { ConnectionString = _connectionString })
{
using (var cmd = new SqlCommand() { Connection = cn })
{
// for your code use one line, no string concatenation
cmd.CommandText = $"SELECT TOP ({recordCount}) ProductName " +
"FROM (SELECT TOP (10) ProductName FROM dbo.Products) a " +
"ORDER BY NEWID();";
cn.Open();
}
}
return productList;
}
public static List<string> GetRandomProducts(int recordCount = 3)
{
List<string> productList = new();
using (var cn = new SqlConnection() { ConnectionString = _connectionString })
{
using (var cmd = new SqlCommand() { Connection = cn })
{
// for your code use one line, no string concatenation
cmd.CommandText = $"SELECT TOP ({recordCount}) ProductName " +
"FROM (SELECT TOP (10) ProductName FROM dbo.Products) a " +
"ORDER BY NEWID();";
cn.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
productList.Add(reader.GetString(0));
}
}
}
return productList;
}
using SqlServerCourseLibrary.Classes;
[TestMethod]
public void SqlRandomRecords()
{
var products = Operations.GetRandomProducts();
Assert.AreEqual(products.Count, 3);
}
[TestMethod]
public void SqlRandomRecords1()
{
var products = Operations.GetRandomProducts();
}
[TestMethod]
public void SqlRandomRecords1()
{
var products = Operations.GetRandomProducts();
if (products.Contains("Uncle Bob's Organic Dried Pears"))
{
Debug.WriteLine("Found 'Uncle Bob's'");
}
else if (products.Contains("Aniseed Syrup"))
{
Debug.WriteLine("Found 'Aniseed Syrup'");
}
else if (products.Contains("Grandma's Boysenberry Spread"))
{
Debug.WriteLine("Found 'Grandma's Boysenberry Spread'");
}
else
{
Debug.WriteLine("Nothing found");
}
}
[TestMethod]
public void SqlRandomRecords2()
{
var products = Operations.GetRandomProducts();
List<string> expectedList = new List<string>()
{
"Uncle Bob's Organic Dried Pears",
"Aniseed Syrup",
"Grandma's Boysenberry Spread"
};
}
[TestMethod]
public void SqlRandomRecords2()
{
var products = Operations.GetRandomProducts();
List<string> expectedList = new List<string>()
{
"Uncle Bob's Organic Dried Pears",
"Aniseed Syrup",
"Grandma's Boysenberry Spread"
};
bool hasMatch = products.Any(product1 =>
expectedList.Any(product2 => product2 == product1));
}
[TestMethod]
public void SqlRandomRecords2()
{
var products = Operations.GetRandomProducts();
List<string> expectedList = new List<string>()
{
"Uncle Bob's Organic Dried Pears",
"Aniseed Syrup",
"Grandma's Boysenberry Spread"
};
bool hasMatch = products.Any(product1 =>
expectedList.Any(product2 => product2 == product1));
Debug.WriteLine(hasMatch);
}