<Window x:Class="DataBindingExam.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="344.636" Width="365.422">
    <Grid Name="grid">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Row="0" Grid.ColumnSpan="2" Grid.Column="0" Margin="3" Orientation="Horizontal" VerticalAlignment="Center">
            <TextBlock Margin="3">ID:</TextBlock>
            <TextBox Name="txtID" Margin="3" MinWidth="100"></TextBox>
            <Button Name="btnQuery" Margin="3" MinWidth="50" Click="btnQuery_Click_1">查询</Button>
            <Button Name="btnUpdate" Margin="3" MinWidth="50" Click="btnUpdate_Click_1">更新</Button>
            <Button Name="btnInsert" Margin="3" MinWidth="50" Click="btnInsert_Click_1">插入</Button>
            <Button Name="btnDelete" Margin="3" MinWidth="50" Click="btnDelete_Click_1">删除</Button>
        </StackPanel>
        <TextBlock Margin="3" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center">CategoryID</TextBlock>
        <TextBox Name="txtCategoryID" Margin="3" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center"

Text="{Binding Path=CategoryID}"></TextBox>

<TextBlock Margin="3" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center">ModelNumber</TextBlock>
        <TextBox Name="txtModelNumber" Margin="3" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center"
                 Text="{Binding Path=ModelNumber}"></TextBox>

        <TextBlock Margin="3" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center">ModelName</TextBlock>
        <TextBox Name="txtModelName" Margin="3" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center"
                 Text="{Binding Path=ModelName}"></TextBox>

 <TextBlock Margin="3" Grid.Row="4" Grid.Column="0" VerticalAlignment="Center">ProductImage</TextBlock>
        <TextBox Name="txtProductImage" Margin="4" Grid.Row="4" Grid.Column="1" VerticalAlignment="Center"
                 Text="{Binding Path=ProductImage}"></TextBox>

        <TextBlock Margin="3" Grid.Row="5" Grid.Column="0" VerticalAlignment="Center">UnitCost</TextBlock>
        <TextBox Name="txtUnitCost" Margin="3" Grid.Row="5" Grid.Column="1" VerticalAlignment="Center"
                 Text="{Binding Path=UnitCost}"></TextBox>

        <TextBox Name="txtDescription" Margin="4" Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="2"
                 Text="{Binding Path=Description}" TextWrapping="Wrap"></TextBox>
    </Grid>

</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary
{
    public class Product
    {
        public int ProductID { get; set; }
        public int CategoryID { get; set; }
        public string ModelNumber { get; set; }
        public string ModelName { get; set; }
        public string ProductImage { get; set; }
        public decimal UnitCost { get; set; }
        public string Description { get; set; }

        public Product(int CategoryID = 0, string ModelNumber = "",
            string ModelName = "", string ProductImage = "", decimal UnitCost=0,string Description="")
        {
            this.CategoryID = CategoryID;
            this.ModelNumber = ModelNumber;
            this.ModelName = ModelName;
            this.ProductImage = ProductImage;
            this.UnitCost = UnitCost;
            this.Description = Description;
        }

    }

}

using ClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DataBindingExam
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnQuery_Click_1(object sender, RoutedEventArgs e)
        {
            int id = Convert.ToInt32(txtID.Text);
            Product p = StoreDB.GetProductByID(id);
            grid.DataContext = p;
        }

        private void btnUpdate_Click_1(object sender, RoutedEventArgs e)
        {
            int categoryID=Convert.ToInt32(txtCategoryID.Text);
            decimal unitCost = Convert.ToDecimal(txtUnitCost.Text);

            Product p = new Product()
            {
                CategoryID = categoryID,
                ModelNumber = txtModelNumber.Text,
                ModelName = txtModelName.Text,
                ProductImage = txtProductImage.Text,
                UnitCost = unitCost,
                Description = txtDescription.Text
            };
            int productID = Convert.ToInt32(txtID.Text);
            StoreDB.UpdateProductByID(productID, p);
        }

        private void btnInsert_Click_1(object sender, RoutedEventArgs e)
        {
            int categoryID = Convert.ToInt32(txtCategoryID.Text);
            decimal unitCost = Convert.ToDecimal(txtUnitCost.Text);
            Product p = new Product()
            {
                CategoryID = categoryID,
                ModelNumber = txtModelNumber.Text,
                ModelName = txtModelName.Text,
                ProductImage = txtProductImage.Text,
                UnitCost = unitCost,
                Description = txtDescription.Text
            };
            StoreDB.InsertProduct(p);
        }

        private void btnDelete_Click_1(object sender, RoutedEventArgs e)
        {
            int productID = Convert.ToInt32(txtID.Text);
            StoreDB.DeleteProductByID(productID);
        }
    }

}

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary
{
    public class StoreDB
    {
        public static string connString = Properties.Settings.Default.ConnectionString;

        public static Product GetProductByID(int id)
        {
            Product p = null;
            SqlConnection con = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand("GetProductByID", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ProductID", id);
            try
            {
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    p = new Product()
                    {
                        CategoryID = (int)reader[1],
                        ModelNumber = reader[2].ToString(),
                        ModelName = reader[3].ToString(),
                        ProductImage=reader[4].ToString(),
                        UnitCost = (decimal)reader[5],
                        Description = reader[6].ToString()
                    };
                }
                return p;

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }

        public static void UpdateProductByID(int ProductID,Product p)
        {
            SqlConnection con = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand("UpdateProductByID", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ProductID",ProductID);
            cmd.Parameters.AddWithValue("@CategoryID",p.CategoryID);
            cmd.Parameters.AddWithValue("@ModelNumber",p.ModelNumber);
            cmd.Parameters.AddWithValue("@ModelName",p.ModelName);
            cmd.Parameters.AddWithValue("@ProductImage",p.ProductImage);
            cmd.Parameters.AddWithValue("@UnitCost",p.UnitCost);
            cmd.Parameters.AddWithValue("@Description",p.Description);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }

        public static void InsertProduct(Product p)
        {
            SqlConnection con = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand("InsertProduct", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@CategoryID", p.CategoryID);
            cmd.Parameters.AddWithValue("@ModelNumber", p.ModelNumber);
            cmd.Parameters.AddWithValue("@ModelName", p.ModelName);
            cmd.Parameters.AddWithValue("@ProductImage", p.ProductImage);
            cmd.Parameters.AddWithValue("@UnitCost", p.UnitCost);
            cmd.Parameters.AddWithValue("@Description", p.Description);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }

        public static void DeleteProductByID(int id)
        {
            SqlConnection con = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand("DeleteProductByID", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ProductID", id);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }
    }

}

USE [store]
GO
/****** Object:  StoredProcedure [dbo].[InsertProduct]    Script Date: 2018/3/29 14:36:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[InsertProduct]
-- Add the parameters for the stored procedure here
(
@CategoryID int,
@ModelNumber varchar(50),
@ModelName varchar(50),
@ProductImage varchar(50),
@UnitCost money,
@Description nvarchar(3800)
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- Insert statements for procedure here
insert into Products(CategoryID,ModelNumber,ModelName,ProductImage,UnitCost,Description)
values(@CategoryID,@ModelNumber,@ModelName,@ProductImage,@UnitCost,@Description);

END

USE [store]
GO
/****** Object:  StoredProcedure [dbo].[DeleteProductByID]    Script Date: 2018/3/29 14:37:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[DeleteProductByID]
(
@ProductID int
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    delete from Products where ProductID=@ProductID;

END

USE [store]
GO
/****** Object:  StoredProcedure [dbo].[UpdateProductByID]    Script Date: 2018/3/29 14:38:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[UpdateProductByID] 
(
@ProductID int,
@CategoryID int,
@ModelNumber varchar(50),
@ModelName varchar(50),
@ProductImage varchar(50),
@UnitCost money,
@Description nvarchar(3800)
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- Insert statements for procedure here
update Products set CategoryID=@CategoryID,ModelNumber=@ModelNumber,ModelName=@ModelName,
ProductImage=@ProductImage,UnitCost=@UnitCost,Description=@Description where ProductID=@ProductID;
END

WPF 数据库增删改查的更多相关文章

  1. Yii2.0高级框架数据库增删改查的一些操作(转)

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...

  2. 2. MongoDB基本操作 —— 用Mongo.exe操作数据库增删改查

    一.开篇 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象 ...

  3. go——beego的数据库增删改查

    一直都不理解使用go语言的时候,为什么还要自己去装beego,以为使用go便可以解决所有的问题,结果在朋友的点拨下,才意识到: go与beego的关系就好比是nodejs与thinkjs的关系,因此也 ...

  4. (转)SQLite数据库增删改查操作

    原文:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html SQLite数据库增删改查操作 一.使用嵌入式关系型SQLite数 ...

  5. Yii2.0高级框架数据库增删改查的一些操作

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...

  6. WindowsPhone8 数据库增删改查

    今天第一次在博客园发表文章,如果有的地方写的不对,还请大家指出! 1.这就是一个简单wp8数据库增删改查 1.创建数据表Person [Table] public class Person : INo ...

  7. MVC——数据库增删改查(Razor)——Html语法

    一.显示界面 .Models(模板) private MyDBDataContext _context = new MyDBDataContext(); public List<Info> ...

  8. MVC——数据库增删改查(Razor)

    一.显示信息 .Models(模板) private MyDBDataContext _context = new MyDBDataContext(); //定义一个变量取出所有数据 public L ...

  9. MVC——数据库增删改查(aspx)

    MVC: V(View) :视图→就是页面的模板 C(Control): 控制器→客户主要面对的就是控制器, M(Model):模板→在模板里面主要就是写关于数据库的各种增删改查的方法 它们之间的关系 ...

随机推荐

  1. Use Word 2010's Navigation Pane to quickly reorganize documents

    Use Word 2010's Navigation Pane to quickly reorganize documents http://www.techrepublic.com/blog/mic ...

  2. [Angular] Adding keyboard events to our control value accessor component

    One of the most important thing when building custom form component is adding accessbility support. ...

  3. Android EditText文本内容变化监听方法

    package com.google; import android.app.Activity; import android.os.Bundle; import android.text.Edita ...

  4. [NPM] Use package.json variables in npm scripts

    In this lesson we will show that you can leverage values that you already have provided in your pack ...

  5. Android-通过Java代码来实现属性动画

    Android-通过Java代码来实现属性动画 除了能够使用定义xml文件来设置动画之外.还能够使用java代码来进行控制动画. 示比例如以下: 布局文件: <RelativeLayout xm ...

  6. 【hdu 1527】取石子游戏

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...

  7. STL源代码学习--vector用法汇总

    一.容器vector 使用vector你必须包含头文件<vector>: #include<vector> 型别vector是一个定义于namespace std内的templ ...

  8. 【noip模拟】德充符

    时间限制:2s 内存限制:512MB [题目描述] 申徒嘉和郑子产都是伯昏无人的学生,子产因为申徒嘉是残疾人,非常看不起他,于是想要刁难他. 子产给了申徒嘉 n个数 a1,a2...an. 现在他要求 ...

  9. spark 分组取topn

    java /** *分组取topn,有序数列去除一些项后,仍然有序,所以应当先排序后分组 *@author Tele * */ public class TopDemo2 { private stat ...

  10. linux 分发同步脚本与分发命令脚本

    同步脚本,在第5步要拼接自己配置的主机名 #!/bin/bash # 获取输入参数个数,如果没有参数,直接退出 pcount=$# )); then echo no args; exit; fi # ...