class ProductDB { // Get the connection string from an external file public stat
ID: 3825587 • Letter: C
Question
class ProductDB
{
// Get the connection string from an external file
public static string ConnectionString()
{
return File.ReadAllText(@"....DBConnStr.txt", Encoding.UTF8);
}
// Get a connection to DB
public static OleDbConnection GetConnection()
{
// Declare a connection and initialze to null
OleDbConnection conn = null;
// Declare string and call method to get connection string from file
string connStr = File.ReadAllText(@"....DBConnStr.txt", Encoding.UTF8);
conn = new OleDbConnection(connStr);
// Show connection string in message box
// MessageBox.Show(connStr); // For debugging
// Instantiate connection
// Return connection
return conn;
}
// Get a list of all Products from DB
public static List<Product> GetProducts()
{
// Declare a List of Product and initialize to null pointer
List<Product> pl = null;
using (ProductsDB = new ProductDB())
{
List<Product> products = db.Products.where(x => x.ProductTypeID == ProductTypeId).toList()
return products;
}
// Declare and get connection
// Declare a string to contain the sql statement
string sqlStr = "";
// Show sql string in message box
MessageBox.Show(sqlStr); // For debugging
// Declare and instantiate a command
// Read all Products from DB in try-catch block
// Try
// Instantiate List of Product
// Open connection
// Execute the command reader
// Loop to get all records and copy to List of Product
// Instantiate Product with constructor, passing data from reader
// Add Product to List of Product
// Catch
// Catch exception
// MessageBox.Show("Exception: " + ex.ToString());
// Set List of Product to null pointer
// Finally
// If connection not null close it
// Return Products List
return pl;
}
Explanation / Answer
Task 1:
Question 1:
class Product
{
public int Id { get; set; }
public string Type { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public void Product(int id, string Type, string Name, double price)
{
this.Id = id;
this.Type = Type;
this.Name = Name;
this.Price = price;
}
public void Product(Product p)
{
this.Id = p.Id;
this.Type = p.Type;
this.Name = p.Name;
this.Price = p.Price;
}
}
Question 2:
class Hoagie : Product
{
public string lettuce { get; set; }
public string tomato { get; set; }
public string onion { get; set; }
public void Hoagie(int id, string Type, string Name, double price,string Lettuce,string Tomato,string Onion)
{
this.Id = id;
this.Type = Type;
this.Name = Name;
this.Price = price;
this.lettuce = Lettuce;
this.tomato = Tomato;
this.onion = Onion;
}
public void Hoagie(Product p, string Lettuce, string Tomato, string Onion)
{
this.Id = p.Id;
this.Type = p.Type;
this.Name = p.Name;
this.Price = p.Price;
this.lettuce = Lettuce;
this.tomato = Tomato;
this.onion = Onion;
}
}
Question 3:
// Declare these two below identifiers in main method
Product p = new Product();
List<Product> pl = new List<Product>();
Question 4: Sorry for this I need entire .cs code.
Note : I just created according your requirement. You need to integrate in your program carefully
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.