0% found this document useful (0 votes)
11 views4 pages

Northwind Demo C# LINQ 21 Feb 2025

The document outlines the development of a C# application called Northwind Demo, utilizing MySQL, ADO.NET, and LINQ. It provides a step-by-step guide starting from basic database connectivity to implementing advanced features like CRUD operations and data filtering. The final integration emphasizes a 3-tier architecture for a robust application structure.

Uploaded by

sogeti2262
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Northwind Demo C# LINQ 21 Feb 2025

The document outlines the development of a C# application called Northwind Demo, utilizing MySQL, ADO.NET, and LINQ. It provides a step-by-step guide starting from basic database connectivity to implementing advanced features like CRUD operations and data filtering. The final integration emphasizes a 3-tier architecture for a robust application structure.

Uploaded by

sogeti2262
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Northwind Demo C# LINQ

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:

1. Basic Setup: Connect to the MySQL database using ADO.NET.


2. Basic Data Retrieval with ADO.NET: Querying data (e.g., Customers).
3. Data Filtering and Paging with ADO.NET: Implementing LINQ for filtering and paging.
4. Advanced LINQ: Complex querying using LINQ to map database relationships.
5. CRUD Operations: Implement Create, Read, Update, and Delete with ADO.NET and LINQ.
6. Final Integration: Putting it all together in a 3-tier architecture.

Step 1: Basic Setup (Connecting to MySQL Database)

Create a simple ADO.NET connection to the MySQL Northwind database.

csharp

using System;
using MySql.Data.MySqlClient;

public class DatabaseHelper


{
private string connectionString;

public DatabaseHelper()
{
connectionString = "Server=your_server;Database=northwind;Uid=your_user;Pwd=your_password;";
}

public MySqlConnection GetConnection()


{
return new MySqlConnection(connectionString);
}
}

Step 2: Basic Data Retrieval with ADO.NET

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 class Customer


{
public int CustomerID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
}

public class CustomerRepository


{
private DatabaseHelper dbHelper;

public CustomerRepository()
{
dbHelper = new DatabaseHelper();
}

public List<Customer> GetAllCustomers()


{
var customers = new List<Customer>();
using (var connection = dbHelper.GetConnection())
{

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();

foreach (var customer in customers)


{
Console.WriteLine($"ID: {customer.CustomerID}, Company: {customer.CompanyName}, Contact: {customer.ContactName}");
}
}
}

Step 3: Data Filtering and Paging with ADO.NET and LINQ

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 class CustomerRepository


{
private DatabaseHelper dbHelper;

public CustomerRepository()
{
dbHelper = new DatabaseHelper();
}

public List<Customer> GetFilteredCustomers(string country, int page = 1, int pageSize = 10)


{
var customers = new List<Customer>();
using (var connection = dbHelper.GetConnection())
{
connection.Open();
string query = "SELECT CustomerID, CompanyName, ContactName, Country FROM Customers WHERE Country = @country";
using (var cmd = new MySqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@country", country);
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.Skip((page - 1) * pageSize).Take(pageSize).ToList();


}
}

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}");
}
}
}

Step 4: Advanced LINQ with Relationships

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 class Order


{
public int OrderID { get; set; }
public DateTime OrderDate { get; set; }
public List<OrderDetail> OrderDetails { get; set; }
}

public class OrderDetail


{
public int ProductID { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
}

public class OrderRepository


{
private DatabaseHelper dbHelper;

public OrderRepository()
{
dbHelper = new DatabaseHelper();
}

public List<Order> GetOrdersForCustomer(int customerId)


{
var orders = new List<Order>();

using (var connection = dbHelper.GetConnection())


{
connection.Open();
string query = "SELECT OrderID, OrderDate FROM Orders WHERE CustomerID = @customerId";
using (var cmd = new MySqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@customerId", customerId);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
var order = new Order
{
OrderID = reader.GetInt32("OrderID"),
OrderDate = reader.GetDateTime("OrderDate"),
OrderDetails = GetOrderDetails(reader.GetInt32("OrderID"))
};
orders.Add(order);
}
}
}

return orders;
}

private List<OrderDetail> GetOrderDetails(int orderId)


{
var orderDetails = new List<OrderDetail>();

using (var connection = dbHelper.GetConnection())


{
connection.Open();
string query = "SELECT ProductID, Quantity, UnitPrice FROM OrderDetails WHERE OrderID = @orderId";
using (var cmd = new MySqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@orderId", orderId);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
orderDetails.Add(new OrderDetail
{
ProductID = reader.GetInt32("ProductID"),
Quantity = reader.GetInt32("Quantity"),
UnitPrice = reader.GetDecimal("UnitPrice")
});
}
}
}

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

foreach (var order in orders)


{
Console.WriteLine($"Order ID: {order.OrderID}, Date: {order.OrderDate}");
foreach (var detail in order.OrderDetails)
{
Console.WriteLine($" Product: {detail.ProductID}, Quantity: {detail.Quantity}, Price: {detail.UnitPrice}");
}
}
}
}

Step 5: CRUD Operations (Create, Read, Update, Delete)

Now, let’s implement basic CRUD operations. Let’s focus on creating and updating customers.

csharp

public class CustomerRepository


{
private DatabaseHelper dbHelper;

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();
}
}
}
}

Step 6: Final Integration (3-Tier Architecture)

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

You might also like