Northwind Demo C# LINQ 21 Feb 2025
Northwind Demo C# LINQ 21 Feb 2025
Show me advanced examples of MySQL, Northwind, ADO.NET, LINQ in a C# application. Each example is to build on the previous example. The whole
lot is to forma a coherent whole. The application is to be called Northwind demo
To build an advanced C# application called Northwind Demo using MySQL, ADO.NET, and LINQ, I'll guide you through a series of examples that will start
with basic functionality and build upon it, eventually forming a full-featured application. This approach ensures that each step is an extension of the
previous one, helping you understand how to structure, access, and manipulate data.
Prerequisites
1. MySQL Database: The Northwind database is assumed to be available in a MySQL instance. You can either download a MySQL version of the
Northwind database or use a converter to port it.
2. C# Development Environment: You will need Visual Studio or another IDE with support for C# development.
Steps Overview:
csharp
using System;
using MySql.Data.MySqlClient;
public DatabaseHelper()
{
connectionString = "Server=your_server;Database=northwind;Uid=your_user;Pwd=your_password;";
}
Now, let's query the database to fetch customer data using ADO.NET.
csharp
using System;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
public CustomerRepository()
{
dbHelper = new DatabaseHelper();
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/4
connection.Open();
string query = "SELECT CustomerID, CompanyName, ContactName FROM Customers";
using (var cmd = new MySqlCommand(query, connection))
{
var reader = cmd.ExecuteReader();
while (reader.Read())
{
customers.Add(new Customer
{
CustomerID = reader.GetInt32("CustomerID"),
CompanyName = reader.GetString("CompanyName"),
ContactName = reader.GetString("ContactName")
});
}
}
}
return customers;
}
}
In your Main method, you can now retrieve the customer data:
csharp
class Program
{
static void Main()
{
var customerRepo = new CustomerRepository();
var customers = customerRepo.GetAllCustomers();
We can now extend this by adding basic filtering and paging capabilities using LINQ. Let’s assume you want to filter customers by country and implement
paging.
csharp
using System.Linq;
public CustomerRepository()
{
dbHelper = new DatabaseHelper();
}
Now, we can test the method with filtering by country and paging:
csharp
class Program
{
static void Main()
{
var customerRepo = new CustomerRepository();
var customers = customerRepo.GetFilteredCustomers("Germany", page: 1, pageSize: 5);
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/4
foreach (var customer in customers)
{
Console.WriteLine($"ID: {customer.CustomerID}, Company: {customer.CompanyName}, Contact: {customer.ContactName}");
}
}
}
Next, we can add a more complex example, where we map database relationships using LINQ. For example, let's retrieve all orders for a specific customer
and their details.
csharp
public OrderRepository()
{
dbHelper = new DatabaseHelper();
}
return orders;
}
return orderDetails;
}
}
You can now retrieve orders along with their details for a customer like this:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/4
csharp
class Program
{
static void Main()
{
var orderRepo = new OrderRepository();
var orders = orderRepo.GetOrdersForCustomer(1); // Assuming CustomerID 1
Now, let’s implement basic CRUD operations. Let’s focus on creating and updating customers.
csharp
public CustomerRepository()
{
dbHelper = new DatabaseHelper();
}
// Create a customer
public void AddCustomer(Customer customer)
{
using (var connection = dbHelper.GetConnection())
{
connection.Open();
string query = "INSERT INTO Customers (CustomerID, CompanyName, ContactName) VALUES (@CustomerID, @CompanyName, @ContactName)";
using (var cmd = new MySqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@CustomerID", customer.CustomerID);
cmd.Parameters.AddWithValue("@CompanyName", customer.CompanyName);
cmd.Parameters.AddWithValue("@ContactName", customer.ContactName);
cmd.ExecuteNonQuery();
}
}
}
// Update a customer
public void UpdateCustomer(Customer customer)
{
using (var connection = dbHelper.GetConnection())
{
connection.Open();
string query = "UPDATE Customers SET CompanyName = @CompanyName, ContactName = @ContactName WHERE CustomerID = @CustomerID";
using (var cmd = new MySqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@CustomerID", customer.CustomerID);
cmd.Parameters.AddWithValue("@CompanyName", customer.CompanyName);
cmd.Parameters.AddWithValue("@ContactName", customer.ContactName);
cmd.ExecuteNonQuery();
}
}
}
}
For the final step, you’ll want to ensure that the application adheres to a 3-tier architecture (Data, Business, Presentation). You can implement a basic UI
(for example, Windows Forms or Console) where you’ll invoke these repositories and services.
This step would involve more UI design and event handling. For simplicity, you can use `CustomerRepository` and `OrderRepository` as services in a
Windows Forms app to call from the UI layer.
By following these steps, you'll build a robust Northwind Demo application that integrates MySQL, ADO.NET, and LINQ with the necessary CRUD
functionality, filtering, paging, and relationship mapping, all encapsulated in a 3-tier design. Would you like more examples on any specific part, like the
presentation layer or UI design?
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/4