......

承接系列四后续:

首先,我要在用户控件2中添加“保存”,“删除”按钮。

XAML代码:

<UserControl x:Class="User.uc_item"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="1920">
<Viewbox>
<StackPanel Width="1920" Height="50" Orientation="Horizontal" HorizontalAlignment="Left">
<TextBox x:Name="tb_id" Width="120" FontSize="20" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="1"/>
<TextBox x:Name="tb_uploader" Width="120" FontSize="20" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="1"/>
<TextBox x:Name="cb_type2" Width="120" FontSize="20" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="1"/>
<ComboBox x:Name="cb_type" Width="120" FontSize="20" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="1"/>
<TextBox x:Name="tb_describe" Width="660" FontSize="20" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="1"/>
<DatePicker x:Name="dp_date" Width="220" FontSize="18" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="1"/>
<DatePicker x:Name="dp_date2" Width="220" FontSize="18" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="1"/>
<Button x:Name="btn_save" FontSize="20" FontWeight="Bold" Width="80" Height="50" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="1" Content="保存" Click="btn_save_Click" Background="Bisque"/>
<Button x:Name="btn_delete" FontSize="20" FontWeight="Bold" Width="80" Height="50" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="1" Content="删除" Click="btn_delete_Click" Background="Bisque"/>
</StackPanel>
</Viewbox>
</UserControl>

然后,编辑“保存按钮”和“删除按钮”的后台代码,前提是编写Word表的类,如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data; namespace User.sqlHelper
{
[Serializable]
class Word
{
public Word()
{ } #region model private int _wordID;
private int _submitterID;
private string _currentStatus;
private string _submitStatus;
private string _wordDescribe;
private DateTime _submitTime;
private DateTime _lastTime; public int WordID
{
set { _wordID = value; }
get { return _wordID; }
} public int SubmitterID
{
set { _submitterID = value; }
get { return _submitterID; }
} public string CurrentStatus
{
set { _currentStatus = value; }
get { return _currentStatus; }
} public string SubmitStatus
{
set { _submitStatus = value; }
get { return _submitStatus; }
} public string WordDescribe
{
set { _wordDescribe = value; }
get { return _wordDescribe; }
} public DateTime SubmitTime
{
set { _submitTime = value; }
get { return _submitTime; }
} public DateTime LastTime
{
set { _lastTime = value; }
get { return _lastTime; }
} #endregion model #region model public Word(int wordID)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select WordID,SubmitterID,CurrentStatus,SubmitStatus,WordDescribe,SubmitTime,LastTime ");
strSql.Append(" FROM [Word] ");
strSql.Append(" where WordID=@wordID"); SqlParameter[] parameter = {
new SqlParameter("@wordID",SqlDbType.Int, )};
parameter[].Value = wordID; DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameter);
DataTable dt = ds.Tables[];
if(ds.Tables[].Rows.Count > )
{
if (dt.Rows[]["WordID"] != null) { this.WordID = int.Parse(dt.Rows[]["WordID"].ToString().Trim()); }
if (dt.Rows[]["SubmitterID"] != null) { this.SubmitterID = int.Parse(dt.Rows[]["SubmitterID"].ToString().Trim()); }
if (dt.Rows[]["CurrentStatus"] != null) { this.CurrentStatus = dt.Rows[]["CurrentStatus"].ToString().Trim(); }
if (dt.Rows[]["SubmitStatus"] != null) { this.SubmitStatus = dt.Rows[]["SubmitStatus"].ToString().Trim(); }
if (dt.Rows[]["WordDescribe"] != null) { this.WordDescribe = dt.Rows[]["WordDescribe"].ToString().Trim(); }
if (dt.Rows[]["SubmitTime"] != null) { this.SubmitTime = DateTime.Parse(dt.Rows[]["SubmitTime"].ToString().Trim()); }
if (dt.Rows[]["LastTime"] != null) { this.LastTime = DateTime.Parse(dt.Rows[]["LastTime"].ToString().Trim()); }
}
} //添加一条数据
public int Add(int submitterid, string currentstatus, string submitstatus, string worddescribe, DateTime submittime, DateTime lasttime)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into [Word] (");
strSql.Append("SubmitterID,CurrentStatus,SubmitStatus,WordDescribe,SubmitTime,LastTime)");
strSql.Append(" values (");
strSql.Append(" @submitterID,@currentStatus,@submitStatus,@wordDescride,@submitTime,@lastTime)"); SqlParameter[] parameter = {
new SqlParameter("@submitterID", SqlDbType.Int, ),
new SqlParameter("@currentStatus", SqlDbType.VarChar, ),
new SqlParameter("@submitStatus", SqlDbType.VarChar, ),
new SqlParameter("@wordDescride", SqlDbType.VarChar, ),
new SqlParameter("@submitTime", SqlDbType.DateTime),
new SqlParameter("@lastTime", SqlDbType.DateTime)};
parameter[].Value = submitterid;
parameter[].Value = currentstatus;
parameter[].Value = submitstatus;
parameter[].Value = worddescribe;
parameter[].Value = submittime;
parameter[].Value = lasttime; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameter);
if (obj == null)
{
return ;
}
else
{
return Convert.ToInt32(obj);
}
} //更新一条数据
public bool Update(int submitterid, string currentstatus, string submitstatus, string worddescribe, DateTime submittime, DateTime lasttime, int wordid)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update [Word] set ");
strSql.Append("SubmitterID=@submitterID,CurrentStatus=@currentStatus,SubmitStatus=@submitStatus,WordDescribe=@wordDescribe,SubmitTime=@submitTime,LastTime=@lastTime");
strSql.Append(" where WordID=@wordID");
SqlParameter[] parameter = {
new SqlParameter("@submitterID",SqlDbType.Int, ),
new SqlParameter("@currentStatus",SqlDbType.VarChar, ),
new SqlParameter("@submitStatus",SqlDbType.VarChar, ),
new SqlParameter("@wordDescribe",SqlDbType.VarChar, ),
new SqlParameter("@submitTime",SqlDbType.DateTime),
new SqlParameter("@lastTime",SqlDbType.DateTime),
new SqlParameter("@wordID",SqlDbType.Int, )};
parameter[].Value = submitterid;
parameter[].Value = currentstatus;
parameter[].Value = submitstatus;
parameter[].Value = worddescribe;
parameter[].Value = submittime;
parameter[].Value = lasttime;
parameter[].Value = wordid; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameter);
if(rows > )
{
return true;
}
else
{
return false;
}
} //删除一条数据
public bool Delete(int wordID)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("delete from [Word] ");
strSql.Append(" where WordID=" + wordID.ToString()); int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
if(rows > )
{
return true;
}
else
{
return false;
}
} //获取表数据
public DataSet GetList()
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * ");
strSql.Append(" FROM [Word]");
return DbHelperSQL.Query(strSql.ToString());
} #endregion model
}
}

对应数据库建的表:

然后编译保存和删除按钮:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using User.sqlHelper;
using System.Data;
using Microsoft.Win32;
using System.IO; namespace User
{
/// <summary>
/// uc_item.xaml 的交互逻辑
/// </summary>
public partial class uc_item : UserControl
{
public uc_item()
{
InitializeComponent();
} //用户控件2下拉框初始值
public void loadCombobox()
{ //申请者状态
ComboBoxItem cbitem1 = new ComboBoxItem();
cb_type.Items.Add(cbitem1);
cbitem1.Content = "已提交"; //审批者状态
ComboBoxItem cbitem2 = new ComboBoxItem();
cb_type.Items.Add(cbitem2);
cbitem2.Content = "已审批"; ComboBoxItem cbitem3 = new ComboBoxItem();
cb_type.Items.Add(cbitem3);
cbitem3.Content = "未审批"; //执行人状态
ComboBoxItem cbitem4 = new ComboBoxItem();
cb_type.Items.Add(cbitem4);
cbitem4.Content = "已执行"; //ComboBoxItem cbitem2 = new ComboBoxItem();
//cb_type.Items.Add(cbitem2);
//cbitem2.Content = "UI"; //ComboBoxItem cbitem3 = new ComboBoxItem();
//cb_type.Items.Add(cbitem3);
//cbitem3.Content = "其他"; ////审批者状态
//ComboBoxItem cbitemL1 = new ComboBoxItem();
//cb_type.Items.Add(cbitemL1);
//cbitemL1.Content = "已审批"; //ComboBoxItem cbitemL2 = new ComboBoxItem();
//cb_type.Items.Add(cbitemL2);
//cbitemL2.Content = "未审批"; ////执行人状态
//ComboBoxItem cbitemR1 = new ComboBoxItem();
//cb_type.Items.Add(cbitemR1);
//cbitemR1.Content = "已执行"; //ComboBoxItem cbitemR2 = new ComboBoxItem();
//cb_result.Items.Add(cbitemR2);
//cbitemR2.Content = "否";
} //初始化表word的id
int wordid = ; //保存按钮
private void btn_save_Click(object sender, RoutedEventArgs e)
{
if (MainWindow.isnew)
{
//查询用户表信息,如果用户名一致,就把id号传递到word表
User_test _u = new User_test();
DataSet _ds = _u.GetList();
if (_ds != null)
{
DataTable _dt = _ds.Tables[];
for (int i = ; i < _dt.Rows.Count; i++)
{
string UserName = _dt.Rows[i]["UserName"].ToString().Trim();
int UserID = int.Parse(_dt.Rows[i]["UserID"].ToString().Trim());
if (this.tb_uploader.Text.Equals(UserName))
{
wordid = UserID;
}
}
} //设置到插入数据到Word表
//初始化Word表
Word _w = new Word();
//当前状态
if (this.cb_type2.Text == "")
{
cb_type2.Text = "未";
}
//提交日期
if (dp_date.Text == "")
{
dp_date.Text = System.DateTime.Now.ToShortTimeString();
}
//最后更改时间
if (dp_date2.Text == "")
{
dp_date2.Text = System.DateTime.Now.ToShortTimeString();
} //插入数据库
_w.Add(wordid, cb_type2.Text, cb_type.Text, tb_describe.Text, System.DateTime.Parse(dp_date.Text), System.DateTime.Parse(dp_date2.Text)); //更新Word表的id
Word _w1 = new Word();
DataSet _ds1 = _w1.GetList();
if (_ds1 != null)
{
DataTable _dt1 = _ds1.Tables[];
for (int i = ; i < _dt1.Rows.Count; i++)
{
this.tb_id.Text = _dt1.Rows[_dt1.Rows.Count - ]["WordID"].ToString().Trim();
}
}
}
else
{
//目的:从User表取出id继续赋给Word表
User_test _u = new User_test();
DataSet _ds = _u.GetList();
if(_ds != null)
{
DataTable _dt = _ds.Tables[];
for (int i = ; i < _dt.Rows.Count; i++)
{
int userid = int.Parse(_dt.Rows[i]["UserID"].ToString().Trim());
string UserName = _dt.Rows[i]["UserName"].ToString().Trim();
if (tb_uploader.Text.Equals(UserName))
{
wordid = userid;
}
}
} //更新word表数据库中
//初始化Word表
Word _w = new Word();
//更新
_w.Update(wordid, cb_type2.Text, cb_type2.Text, tb_describe.Text, System.DateTime.Parse(dp_date.Text), System.DateTime.Parse(dp_date2.Text), int.Parse(tb_id.Text)); }
//设置是否新建为false
MainWindow.isnew = false;
//弹出消息成功
MessageBox.Show("保存成功!");
} //删除按钮
private void btn_delete_Click(object sender, RoutedEventArgs e)
{
Word _w = new Word();
MessageBoxResult mbr = MessageBox.Show("是否确定删除!","删除提示", MessageBoxButton.YesNoCancel);
if (mbr == MessageBoxResult.Yes)
{
//删除数据库对应的值
_w.Delete(int.Parse(tb_id.Text));
//移除用户控件2
(this.Parent as StackPanel).Children.Remove(this);
MessageBox.Show("删除成功!");
}
} }
}

结果图:

点击保存:

查看数据库:

然后,我再点击删除按钮。

点击删除后查看数据库:

数据已经删除。

【WPF学习笔记】之如何保存画面上新建的数据到数据库中并且删除画面上的数据和数据库的数据:动画系列之(五)的更多相关文章

  1. 【WPF学习笔记】之如何点击“新建”按钮,在面板中加载一条条的“用户控件”的信息:动画系列之(四)

    ...... 承接上一系列动画三. 在主界面后台代码设置嵌套第二个用户控件. using System; using System.Collections.Generic; using System. ...

  2. WPF学习笔记-用Expression Design制作矢量图然后导出为XAML

    WPF学习笔记-用Expression Design制作矢量图然后导出为XAML 第一次用Windows live writer写东西,感觉不错,哈哈~~ 1.在白纸上完全凭感觉,想象来画图难度很大, ...

  3. WPF 学习笔记-在WPF下创建托盘图标

    原文:WPF 学习笔记-在WPF下创建托盘图标 首先需要在项目中引用System.Windows.Forms,System.Drawing; using System; using System.Co ...

  4. WPF 学习笔记-设置属性使窗口不可改变大小

    原文:WPF 学习笔记-设置属性使窗口不可改变大小 调整Windows下的ResizeMode属性: ResizeMode = NoResize Resize属性是控制Windows是否可以改变大小, ...

  5. WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决

    原文:WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决 如下图,在凭证编辑窗体中,有的单元格不需要数字,但如果录入数字后再删除,会触发数字验证,单元格显示红色框线,导致不能执行 ...

  6. Yii框架学习笔记(二)将html前端模板整合到框架中

    选择Yii 2.0版本框架的7个理由 http://blog.chedushi.com/archives/8988 刚接触Yii谈一下对Yii框架的看法和感受 http://bbs.csdn.net/ ...

  7. 【转载】【时序约束学习笔记1】Vivado入门与提高--第12讲 时序分析中的基本概念和术语

    时序分析中的基本概念和术语 Basic concept and Terminology of Timing Analysis 原文标题及网址: [时序约束学习笔记1]Vivado入门与提高--第12讲 ...

  8. 【WPF学习笔记】之如何把数据库里的值读取出来然后显示在页面上:动画系列之(六)(评论处有学习资料及源码)

    (应博友们的需要,在文章评论处有源码链接地址,以及WPF学习资料.工具等,希望对大家有所帮助) ...... 承接系列五 上一节讲了,已经把数据保存到数据库并且删除数据,本讲是把已经存在的数据从数据库 ...

  9. WPF学习笔记(3)——style

    http://www.cnblogs.com/Zhouyongh/archive/2011/08/01/2123610.html Style 用来在类型的不同实例之间共享属性.资源和事件处理程序,您可 ...

随机推荐

  1. PE文件格式---节和节表

    17.1.4  节表和节 从排列位置来看,PE文件在DOS部分和PE文件头部分以后就是节表和多个不同的节(如图17.1中的③和④所示).要理解什么是节表,什么是节以及它们之间的关系,那就首先要了解Wi ...

  2. linux内核情景分析之信号实现

    信号在进程间通信是异步的,每个进程的task_struct结构有一个sig指针,指向一个signal_struct结构 定义如下 struct signal_struct { atomic_t cou ...

  3. Hadoop-hdfs安装与配置

    一.安装要求   安装JDK   yum -y install jdk(或手动安装)  设置namenode节点到datanode节点的免密码登陆   a. 本地免密码登录     # ssh loc ...

  4. HDU 6225.Little Boxes-大数加法 (2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学))

    整理代码... Little Boxes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/O ...

  5. Sprak RDD简单应用

    来自:http://my.oschina.net/scipio/blog/284957#OSC_h5_11 目录[-] 1.准备文件 2.加载文件 3.显示一行 4.函数运用 (1)map (2)co ...

  6. 洛谷——P1119 灾后重建

    P1119 灾后重建 题目背景 B地区在地震过后,所有村庄都造成了一定的损毁,而这场地震却没对公路造成什么影响.但是在村庄重建好之前,所有与未重建完成的村庄的公路均无法通车.换句话说,只有连接着两个重 ...

  7. BZOJ 4543 2016北京集训测试赛(二)Problem B: thr 既 长链剖分学习笔记

    Solution 这题的解法很妙啊... 考虑这三个点可能的形态: 令它们的重心为距离到这三个点都相同的节点, 则其中两个点分别在重心的两棵子树中, 且到重心的距离相等; 第三个点可能在重心的一棵不同 ...

  8. maxwell简单部署使用

    详细资料可以参考maxwell官网  (mysql + maxwell + kafka + elasticsearch) 说明:本文主要是关于配置maxwell监听mysql的数据修改并实时将修改内容 ...

  9. 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用

    2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...

  10. python局部变量与全局变量

    name = "head first python"def what_happens_here():    print(name)  1    name = "pytho ...