WPF【10_2】數據庫與WPF實戰-示例

客戶預約關聯示例圖

MainWindow.xaml 代碼
<Window x:Class="WPF_CMS.MainWindow"
? ? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
? ? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
? ? xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
? ? xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
? ? xmlns:local="clr-namespace:WPF_CMS"
? ? mc:Ignorable="d"
? ? Title="MainWindow" Height="450" Width="800">
<Grid>
? ? <Label Content="客戶列表" HorizontalAlignment="Left" Margin="32,22,0,0" VerticalAlignment="Top"/>
? ? <ListBox Name="customerList" HorizontalAlignment="Left" Height="229" Margin="32,61,0,0" VerticalAlignment="Top" Width="249" SelectionChanged="customerList_SelectionChanged"/>
? ? <Label Content="預約記錄" HorizontalAlignment="Left" Margin="444,22,0,0" VerticalAlignment="Top"/>
? ? <ListBox Name="appointmentList" HorizontalAlignment="Left" Height="229" Margin="444,61,0,0" VerticalAlignment="Top" Width="249"/>
? ? <Button Content="刪除客戶" HorizontalAlignment="Left" Margin="32,306,0,0" VerticalAlignment="Top" Width="249" Click="DeleteCustomer_Click"/>
? ? <Button Content="取消預約" HorizontalAlignment="Left" Margin="444,306,0,0" VerticalAlignment="Top" Width="249" Click="DeleteAppointment_Click"/>
? ? <TextBox Name="NameTextBox" HorizontalAlignment="Left" Margin="32,359,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
? ? <TextBox Name="IdTextBox" HorizontalAlignment="Left" Margin="322,359,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
? ? <TextBox Name="AddressTextBox" HorizontalAlignment="Left" Margin="175,359,0,0" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
? ? <Label Content="姓名" HorizontalAlignment="Left" Margin="32,331,0,0" VerticalAlignment="Top"/>
? ? <Label Content="身份證" HorizontalAlignment="Left" Margin="175,333,0,0" VerticalAlignment="Top"/>
? ? <Label Content="住址" HorizontalAlignment="Left" Margin="322,331,0,0" VerticalAlignment="Top"/>
? ? <Button Content="添加客戶" HorizontalAlignment="Left" Margin="32,382,0,0" VerticalAlignment="Top" Click="AddCustomer_Click"/>
? ? <DatePicker Name="AppointmentDatePicker" HorizontalAlignment="Left" Margin="467,356,0,0" VerticalAlignment="Top"/>
? ? <Button Content="預約" HorizontalAlignment="Left" Margin="589,359,0,0" VerticalAlignment="Top" Click="AddAppointment_Click"/>
? ? <Button Content="更新客戶資料" HorizontalAlignment="Left" Margin="112,387,0,0" VerticalAlignment="Top" Click="UpdateCustomer_Click"/>
</Grid>
</Window>

MainWindow.xaml.cs 代碼
public partial class MainWindow : Window
{
? ? private SqlConnection _sqlConnection;
? ? public MainWindow()
? ? {
? ? ? ? InitializeComponent();
? ? ? ? string connectionString = "Data Source=localhost;Initial Catalog=course565;Persist Security Info=True;User ID=sa;Password=PaSSword12!;Pooling=False";

? ? ? ? _sqlConnection = new SqlConnection(connectionString);

? ? ? ? ShowCustomers();
? ? }

? ? private void ShowCustomers()
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select * from Customers", _sqlConnection);

? ? ? ? ? ? using (sqlDataAdapter)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? DataTable customerTable = new DataTable();
? ? ? ? ? ? ? ? sqlDataAdapter.Fill(customerTable);

? ? ? ? ? ? ? ? customerList.DisplayMemberPath = "Name";
? ? ? ? ? ? ? ? customerList.SelectedValuePath = "Id";
? ? ? ? ? ? ? ? customerList.ItemsSource = customerTable.DefaultView;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? catch (Exception e)
? ? ? ? {
? ? ? ? ? ? MessageBox.Show(e.ToString());
? ? ? ? }
? ? }

? ? private void customerList_SelectionChanged(object sender, SelectionChangedEventArgs e)
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? string query = "select * from Appointments join Customers on Appointments.CustomerId = Customers.Id where Customers.Id = @CustomerId";

? ? ? ? ? ? var customerId = customerList.SelectedValue;
? ? ? ? ? ? if (customerId==null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? appointmentList.ItemsSource = null;
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }

? ? ? ? ? ? DataRowView selectedItem = customerList.SelectedItem as DataRowView;
? ? ? ? ? ? NameTextBox.Text = selectedItem["Name"] as string;
? ? ? ? ? ? IdTextBox.Text = selectedItem["IdNnumber"] as string;
? ? ? ? ? ? AddressTextBox.Text = selectedItem["Address"] as string;

? ? ? ? ? ? SqlCommand sqlCommand = new SqlCommand(query, _sqlConnection);

? ? ? ? ? ? SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);

? ? ? ? ? ? sqlCommand.Parameters.AddWithValue("@CustomerId", customerId);

? ? ? ? ? ? using (sqlDataAdapter)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? DataTable appointmentTable = new DataTable();
? ? ? ? ? ? ? ? sqlDataAdapter.Fill(appointmentTable);

? ? ? ? ? ? ? ? appointmentList.DisplayMemberPath = "Time";
? ? ? ? ? ? ? ? appointmentList.SelectedValuePath = "Id";
? ? ? ? ? ? ? ? appointmentList.ItemsSource = appointmentTable.DefaultView;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? catch (Exception error)
? ? ? ? {
? ? ? ? ? ? MessageBox.Show(error.ToString());
? ? ? ? }
? ? }

? ? private void DeleteAppointment_Click(object sender, RoutedEventArgs e)
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? var sql = "delete from Appointments where Id = @AppointmentId";

? ? ? ? ? ? var appointmentId = appointmentList.SelectedValue;

? ? ? ? ? ? SqlCommand sqlCommand = new SqlCommand(sql, _sqlConnection);
? ? ? ? ? ? sqlCommand.Parameters.AddWithValue("@AppointmentId", appointmentId);

? ? ? ? ? ? _sqlConnection.Open();
? ? ? ? ? ? sqlCommand.ExecuteScalar();
? ? ? ? ? ??
? ? ? ? }
? ? ? ? catch (Exception error)
? ? ? ? {
? ? ? ? ? ? MessageBox.Show(error.ToString());
? ? ? ? }
? ? ? ? finally
? ? ? ? {
? ? ? ? ? ? _sqlConnection.Close();
? ? ? ? ? ? customerList_SelectionChanged(null, null);
? ? ? ? }
? ? }

? ? private void DeleteCustomer_Click(object sender, RoutedEventArgs e)
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? string sqlDeleteAppointment = "delete from Appointments where CustomerId=@CustomerId";
? ? ? ? ? ? string sqlDeleteCustomer = "delete from Customers where id=@CustomerId";

? ? ? ? ? ? var customerId = customerList.SelectedValue;

? ? ? ? ? ? SqlCommand cmd1 = new SqlCommand(sqlDeleteAppointment, _sqlConnection);
? ? ? ? ? ? SqlCommand cmd2 = new SqlCommand(sqlDeleteCustomer, _sqlConnection);

? ? ? ? ? ? cmd1.Parameters.AddWithValue("@CustomerId", customerId);
? ? ? ? ? ? cmd2.Parameters.AddWithValue("@CustomerId", customerId);

? ? ? ? ? ? _sqlConnection.Open();
? ? ? ? ? ? cmd1.ExecuteScalar();
? ? ? ? ? ? cmd2.ExecuteScalar();
? ? ? ? }
? ? ? ? catch (Exception error)
? ? ? ? {
? ? ? ? ? ? MessageBox.Show(error.ToString());
? ? ? ? }
? ? ? ? finally
? ? ? ? {
? ? ? ? ? ? _sqlConnection.Close();
? ? ? ? ? ? ShowCustomers();
? ? ? ? ? ? customerList_SelectionChanged(null, null);
? ? ? ? }
? ? }

? ? private void AddCustomer_Click(object sender, RoutedEventArgs e)
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? var sql = "insert into Customers values (@name, @id, @address)";

? ? ? ? ? ? SqlCommand sqlCommand = new SqlCommand(sql, _sqlConnection);

? ? ? ? ? ? sqlCommand.Parameters.AddWithValue("@name", NameTextBox.Text);
? ? ? ? ? ? sqlCommand.Parameters.AddWithValue("@id", IdTextBox.Text);
? ? ? ? ? ? sqlCommand.Parameters.AddWithValue("@address", AddressTextBox.Text);

? ? ? ? ? ? _sqlConnection.Open();
? ? ? ? ? ? sqlCommand.ExecuteScalar();

? ? ? ? }
? ? ? ? catch (Exception error)
? ? ? ? {
? ? ? ? ? ? MessageBox.Show(error.ToString());
? ? ? ? }
? ? ? ? finally
? ? ? ? {
? ? ? ? ? ? _sqlConnection.Close();
? ? ? ? ? ? ShowCustomers();
? ? ? ? }
? ? }

? ? private void AddAppointment_Click(object sender, RoutedEventArgs e)
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? var sql = "insert into Appointments values (@date, @customerId)";

? ? ? ? ? ? SqlCommand sqlCommand = new SqlCommand(sql, _sqlConnection);

? ? ? ? ? ? sqlCommand.Parameters.AddWithValue("@date", AppointmentDatePicker.Text);
? ? ? ? ? ? sqlCommand.Parameters.AddWithValue("@customerId", customerList.SelectedValue);

? ? ? ? ? ? _sqlConnection.Open();
? ? ? ? ? ? sqlCommand.ExecuteScalar();
? ? ? ? }
? ? ? ? catch (Exception error)
? ? ? ? {
? ? ? ? ? ? MessageBox.Show(error.ToString());
? ? ? ? }
? ? ? ? finally
? ? ? ? {
? ? ? ? ? ? _sqlConnection.Close();
? ? ? ? ? ? customerList_SelectionChanged(null, null);
? ? ? ? }
? ? }

? ? private void UpdateCustomer_Click(object sender, RoutedEventArgs e)
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? var sql = "update Customers set Name=@name, IdNnumber=@idNumber, Address=@address where Id=@customerId";

? ? ? ? ? ? SqlCommand sqlCommand = new SqlCommand(sql, _sqlConnection);
? ? ? ? ? ? sqlCommand.Parameters.AddWithValue("@name", NameTextBox.Text.Trim());
? ? ? ? ? ? sqlCommand.Parameters.AddWithValue("@idNumber", IdTextBox.Text.Trim());
? ? ? ? ? ? sqlCommand.Parameters.AddWithValue("@address", AddressTextBox.Text.Trim());
? ? ? ? ? ? sqlCommand.Parameters.AddWithValue("@customerId", customerList.SelectedValue);

? ? ? ? ? ? _sqlConnection.Open();
? ? ? ? ? ? sqlCommand.ExecuteScalar();

? ? ? ? }
? ? ? ? catch (Exception error)
? ? ? ? {
? ? ? ? ? ? MessageBox.Show(error.ToString());
? ? ? ? }
? ? ? ? finally
? ? ? ? {
? ? ? ? ? ? _sqlConnection.Close();
? ? ? ? ? ? ShowCustomers();
? ? ? ? }
? ? }
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/84767.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/84767.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/84767.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

理解 Kubernetes 的架構與控制平面組件運行機制

文章目錄 K8s架構K8s核心組件控制平面組件&#xff08;部署在 Master 節點&#xff09;1. 查看組件運行情況2. 查看組件 help 命令 Node端組件&#xff08;部署在每個工作節點&#xff09; K8s內部工作原理 Kubernetes&#xff08;也稱為 K8s&#xff09;是一個開源的容器編排和…

Express+MySQL后臺開發實戰:從模塊化到錯誤處理的全鏈路解析

ExpressMySQL后臺開發實戰&#xff1a;從模塊化到錯誤處理的全鏈路解析 摘要&#xff1a;本文將以Node.jsExpress框架為基礎&#xff0c;結合MySQL數據庫實戰&#xff0c;深度剖析后臺系統中數據庫模塊化設計、安全查詢、錯誤處理等核心開發要點。 一、項目環境與技術棧 ├─…

Spring AI 智能體代理模式(Agent Agentic Patterns)

AgentAgenticPatterns 簡介 在最近的一篇研究報告《構建高效代理》 中&#xff0c;Anthropic分享了關于構建高效大語言模型&#xff08;LLM&#xff09;代理的寶貴見解。這項研究特別有趣的地方在于&#xff0c;它強調簡單性和可組合性&#xff0c;而非復雜的框架。讓我們來探…

基于 Vue3 與 exceljs 實現自定義導出 Excel 模板

在開發中&#xff0c;我們需要常常為用戶提供更多的數據錄入方式&#xff0c;Excel 模板導出與導入是一個常見的功能點。本文將介紹如何使用 Vue3、exceljs 和 file-saver 實現一個自定義導出 Excel 模板&#xff0c;并在特定列添加下拉框選擇的數據驗證功能。 技術選型 excelj…

git 命令之-git cherry-pick

今天得到一個通知&#xff0c;這個業務版本里面部分已經開發但還沒測試的內容要新開一個分支提交&#xff0c;但是我已經有幾個提交上去了&#xff0c;難道只能一個一個文件復制到新的分支嗎&#xff1f;我不&#xff0c;我找到了這個git命令&#xff0c;可以解決我的困惑&…

浙大版《Python 程序設計》題目集6-3,6-4,6-5,6-6列表或元組的數字元素求和及其變式(遞歸解法)

目錄 6-3 輸入格式: 輸出格式: 輸入樣例: 輸出樣例: 6-4 輸入格式: 輸出格式: 輸入樣例: 輸出樣例: 6-5 輸入格式: 輸出格式: 輸入樣例: 輸出樣例: 6-6 輸入格式: 輸出格式: 輸入樣例: 輸出樣例: 6-3 第6章-3 列表或元組的數字元素求和 分數 20 全屏瀏覽 切換布局 作者 陳春暉 …

【b站計算機拓荒者】【2025】微信小程序開發教程 - chapter2 小程序核心

1 尺寸單位和樣式 1.1 創建小程序項目-純凈環境 // 該刪的刪掉。 1.2 尺寸單位 # 小程序內 手機屏幕大小可能不一樣&#xff0c;使用px像素就會出現樣式問題 --> 小程序統一了整個寬度&#xff0c;即750rpx&#xff0c;屏幕一半則是375rpx -->因此不管什么手機都可以…

攻防世界逆向刷題筆記(新手模式9-1?)

bad_python 看樣子是pyc文件損壞了。利用工具打開&#xff0c;發現是MAGIC壞了。搜下也沒有頭緒。 攻防世界-難度1- bad_python - _rainyday - 博客園 python Magic Number對照表以及pyc修復方法 - iPlayForSG - 博客園 看WP才知道36已經提示了pyc版本了。參考第二個文章&am…

mysql ACID 原理

序言&#xff1a;ACID 是一組數據庫設計原則&#xff0c;他是業務數據和關鍵業務程序的可靠性保障。 1、atomicity&#xff08;原子性&#xff09; 依賴如下能力 autocommit commit rollback2、一致性 2.1 double write buffer 1、定義&#xff1a;double write buffer 是…

WebStorm 高效快捷方式全解析

作為前端開發的黃金搭檔&#xff0c;WebStorm 憑借強大的功能和高度可定制的快捷鍵體系&#xff0c;成為眾多開發者提升編碼效率的利器。本文基于 IntelliJ IDEA 的快捷鍵體系&#xff08;WebStorm 作為 JetBrains 家族成員&#xff0c;快捷鍵邏輯高度一致&#xff09;&#xf…

基于 STM32 的農村污水處理控制系統設計與實現

摘要 針對農村污水處理自動化程度低、運維成本高的問題,本文設計了一種基于 STM32 單片機的污水處理控制系統。系統通過多傳感器實時監測水質參數,結合 PID 控制算法實現污水處理全流程自動化,并集成遠程監控功能,滿足農村地區低成本、易維護的需求。 一、硬件系統設計 …

自動生成md文件以及config.mjs文件-vitepress

效果&#xff1a; config.mjs文件 import {defineConfig} from vitepress import hljs from highlight.js/lib/core import javascript from highlight.js/lib/languages/javascript import xml from highlight.js/lib/languages/xml import {ref} from "./cache/deps/vue…

Tailwind css實戰,基于Kooboo構建AI對話框頁面(二)

基于上篇內容&#xff0c;添加交互邏輯&#xff0c;實現一個偽聊天功能的對話框效果&#xff1a; Tailwind css實戰&#xff0c;基于Kooboo構建AI對話框頁面&#xff08;一&#xff09;-CSDN博客 在前期文章中&#xff0c;我們完成了 AI 對話框的靜態頁面搭建。本文將聚焦交互…

Conda:環境移植及更新1--使用conda-pack

更多內容&#xff1a;XiaoJ的知識星球 目錄 一、使用conda-pack1.安裝 conda-pack2.移植整個 Anaconda 環境3.移植單個虛擬環境4.驗證是否生效 在相同Linux設備上移植Miniconda3&#xff08;Anaconda3同理&#xff09;常用方法有。 使用conda-pack&#xff1a;使用conda-pack工…

樹莓派超全系列教程文檔--(50)如何查找樹莓派的IP地址

如何查找樹莓派的IP地址 找到您的Raspberry Pi的IP地址桌面命令行引導輸出網絡管理器使用mDNS解析 raspberrypi.local檢查路由器的設備列表使用 nmap 查找設備使用智能手機應用程序查找設備 文章來源&#xff1a; http://raspberry.dns8844.cn/documentation 原文網址 找到您…

如何優化 MySQL 存儲過程的性能?

文章目錄 1. 優化 SQL 語句避免全表掃描減少子查詢&#xff0c;改用 JOIN避免 SELECT 2. 合理使用索引3. 優化存儲過程結構減少循環和臨時變量避免重復計算 4. 使用臨時表和緩存5. 優化事務處理6. 分析和監控性能7. 優化數據庫配置8. 避免用戶自定義函數&#xff08;UDF&#…

尚硅谷redis7 47-48 redis事務之理論簡介

47 redis事務之理論簡介 什么是事務 可以一次執行多個命令,本質是一組命令的集合。一個事務中的所有命令都會序列化,按順序地串行化執行而不會被其它命令插入 能干什么&#xff1f; 一個隊列中&#xff0c;一次性、順序性、排他性的執行一系列操作 redis事務vs數據庫事務 …

Nginx 在四大核心場景中的應用實踐與優化

一、Nginx 核心應用場景深度解析 1. HTTP 服務器&#xff1a;靜態資源的高性能承載者 Nginx 作為 HTTP 服務器時&#xff0c;憑借輕量級架構和高效的事件驅動模型&#xff0c;成為靜態資源服務的首選方案。 核心能力與場景 靜態文件高效處理&#xff1a;直接響應 HTML、CSS…

亞當·斯密思想精髓的數學建模與形式化表征

亞當斯密思想精髓的數學建模與形式化表征 摘要&#xff1a;本文運用數學建模方法對亞當斯密的經濟與倫理思想進行形式化表征。通過分工的規模經濟模型和市場均衡條件展現《國富論》中"看不見的手"原理&#xff1b;采用擴展效用函數與合作博弈均衡解釋《道德情操論》…

FastDFS集群部署與性能優化實戰

目錄 一、介紹 二、FastDFS原理 三、FastDFS部署 1.資源清單 2.修改主機名 3.安裝libfastcommon&#xff08;tracker01、tracker02、storage1、storage2&#xff09; 4.安裝編譯FastDFS&#xff08;tracker01、tracker02、storage1、storage2&#xff09; 5.配置tracker…