......
承接系列四后续:
首先,我要在用户控件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, 4)}; parameter[0].Value = wordID; DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameter); DataTable dt = ds.Tables[0]; if(ds.Tables[0].Rows.Count > 0) { if (dt.Rows[0]["WordID"] != null) { this.WordID = int.Parse(dt.Rows[0]["WordID"].ToString().Trim()); } if (dt.Rows[0]["SubmitterID"] != null) { this.SubmitterID = int.Parse(dt.Rows[0]["SubmitterID"].ToString().Trim()); } if (dt.Rows[0]["CurrentStatus"] != null) { this.CurrentStatus = dt.Rows[0]["CurrentStatus"].ToString().Trim(); } if (dt.Rows[0]["SubmitStatus"] != null) { this.SubmitStatus = dt.Rows[0]["SubmitStatus"].ToString().Trim(); } if (dt.Rows[0]["WordDescribe"] != null) { this.WordDescribe = dt.Rows[0]["WordDescribe"].ToString().Trim(); } if (dt.Rows[0]["SubmitTime"] != null) { this.SubmitTime = DateTime.Parse(dt.Rows[0]["SubmitTime"].ToString().Trim()); } if (dt.Rows[0]["LastTime"] != null) { this.LastTime = DateTime.Parse(dt.Rows[0]["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, 8), new SqlParameter("@currentStatus", SqlDbType.VarChar, 50), new SqlParameter("@submitStatus", SqlDbType.VarChar, 50), new SqlParameter("@wordDescride", SqlDbType.VarChar, 100), new SqlParameter("@submitTime", SqlDbType.DateTime), new SqlParameter("@lastTime", SqlDbType.DateTime)}; parameter[0].Value = submitterid; parameter[1].Value = currentstatus; parameter[2].Value = submitstatus; parameter[3].Value = worddescribe; parameter[4].Value = submittime; parameter[5].Value = lasttime; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameter); if (obj == null) { return 0; } 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, 8), new SqlParameter("@currentStatus",SqlDbType.VarChar, 50), new SqlParameter("@submitStatus",SqlDbType.VarChar, 50), new SqlParameter("@wordDescribe",SqlDbType.VarChar, 100), new SqlParameter("@submitTime",SqlDbType.DateTime), new SqlParameter("@lastTime",SqlDbType.DateTime), new SqlParameter("@wordID",SqlDbType.Int, 8)}; parameter[0].Value = submitterid; parameter[1].Value = currentstatus; parameter[2].Value = submitstatus; parameter[3].Value = worddescribe; parameter[4].Value = submittime; parameter[5].Value = lasttime; parameter[6].Value = wordid; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameter); if(rows > 0) { 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 > 0) { 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 = 1; //保存按钮 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[0]; for (int i = 0; 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[0]; for (int i = 0; i < _dt1.Rows.Count; i++) { this.tb_id.Text = _dt1.Rows[_dt1.Rows.Count - 1]["WordID"].ToString().Trim(); } } } else { //目的:从User表取出id继续赋给Word表 User_test _u = new User_test(); DataSet _ds = _u.GetList(); if(_ds != null) { DataTable _dt = _ds.Tables[0]; for (int i = 0; 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("删除成功!"); } } } }
结果图:
点击保存:
查看数据库:
然后,我再点击删除按钮。
点击删除后查看数据库:
数据已经删除。