這個專案的目標是設計一個應用程式,查詢 Northwind 資料庫中的客戶訂單商品。以下是解決方案的步驟與建議:
你可以使用 ADO.NET 或 Entity Framework 來連接資料庫,根據個人偏好來設計。
string connectionString = "Data Source=你的伺服器名稱;Initial Catalog=Northwind;Integrated Security=True;";
SqlConnection connection = new SqlConnection(connectionString);
Entity Framework:
可以透過 DbContext
使用 LINQ 查詢資料庫,並進行資料處理。
這裡提供一個基本的表單佈局建議:
可以在表單初始化時,將所有客戶載入 ComboBox
:
private void Form1_Load(object sender, EventArgs e)
{
string query = "SELECT CustomerID, CompanyName FROM Customers";
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
DataTable customerTable = new DataTable();
adapter.Fill(customerTable);
cbCustomers.DisplayMember = "CompanyName";
cbCustomers.ValueMember = "CustomerID";
cbCustomers.DataSource = customerTable;
}