WPF 新增

WPF与我们之前做的MVC是不太一样的,因为WPF的服务端Client需要与服务器Service连接,还要在数据库SQL Server里写存储过程(在数据库SQL Server写存储过程,在服务器Service调用存储过程,在客户端Client引用服务器,三者一一嵌套。所以我们的新增与之前的新增不太一样,但逻辑是一样的)

1.在主页面中先把所有字段和按钮设计出来(按钮用Button控件;搜索用TextBlock控件,搜索的文本框用TextBox控件,在搜索框中还可以弹出工具提示对象,用ToolTip属性;表格用DataGridTextColumn。如下图)
在这里插入图片描述
2.查询数据(在SQL Server里用存储过程查询数据,需要把表格的所有字段都查出来)

SELECT    
		ROW_NUMBER() over(order by t_operators.operator_id) AS number,	t_operators.operator_id, t_operators.staff_id, 
		RTRIM(t_staff.staff_name) AS name, 
		RTRIM(t_operators.operator_accounts) AS accounts, 
		RTRIM(t_operators.operator_password) AS password, 
		RTRIM(case WHEN t_operators.effective='true' THEN '有效' ELSE '无效' END) AS effective, 
		RTRIM(t_operators.note) AS note
FROM     t_operators INNER JOIN
		      t_staff ON t_operators.staff_id = t_staff.staff_id

3.在服务端Service调用存储过程(在服务器Service中配置服务,定义操作契约,服务协议)

//实例化对象数组
   SqlParameter[] mySqlParameters =
   {
        //定义传递参数,以及传递参数的类型
        new SqlParameter("@type",SqlDbType.NChar),
   };
  mySqlParameters[0].Value = "UserControl_Loaded_SelectStaffAccountManage";
  DataSet ds = myDALMethod.QueryDataSet("UC_StaffAccountManage", mySqlParameters);
  //返回值
  return ds;

4.在客户端Client中引用服务(初始化表格和绑定数据)

  //初始化表格
  PublicStaticMothd.SetDgStyle(dgAccountManage);
  //调用:绑定表格数据
  SelectdgAccountManage();

整个页面设计如下:
在这里插入图片描述
5.在服务端Service调用存储过程(调用未匹配员工账号的员工信息)

//实例化对象数组
   SqlParameter[] mySqlParameters =
      {
             //定义传递参数,以及传递参数的类型
            new SqlParameter("@type",SqlDbType.NChar),
       };
   mySqlParameters[0].Value = "Window_Loaded_SelectStaff"; //获取执行的存储过程名称           
   DataSet ds = myDALMethod.QueryDataSet("UC_StaffAccountManage", mySqlParameters);
//返回值
  return ds;

6.在客户端Client中绑定下拉框

 DataTable dt = myClient.Window_Loaded_SelectStaff().Tables[0];//获取数据
 cbo_Name.ItemsSource = dt.DefaultView;//绑定数据源
 cbo_Name.SelectedValuePath = "staff_id";//id选中值
 cbo_Name.DisplayMemberPath = "name";//name显示值

7.在客户端ClientXAML中设计新增的页面(点击新增时弹出窗口。有效否使用CheckBox控件)
在这里插入图片描述
在新增时,下拉框是查询未匹配账号的员工信息(下拉框使用ComboBox控件)

SELECT   staff_id, 
		RTRIM(staff_name) AS name
FROM     t_staff
WHERE    staff_id NOT IN(SELECT   t_operators.staff_id FROM t_operators )
//数据库的新增代码(还需要判断新增的账号是否是存在的)
IF exists(select 0 from t_operators where operator_accounts=@operator_accounts)
	begin
		RETURN
	end
ELSE
	begin
INSERT INTO t_operators(staff_id,operator_accounts,operator_password,effective,note)
		 VALUES (@staff_id,@operator_accounts,@operator_password,@effective,@note)
	end
END

在客户端Client,还需要判断新增的账号不能与数据库的账号一样,不能重复;姓名、员工账号、密码不能为空;新增新的账号时是重新注册了一个新的账号
在这里插入图片描述
8.重置按钮时是把它们全部设为空值

cbo_Name.SelectedValue = 0;
txt_Account.Text = string.Empty;
txt_Note.Text = string.Empty;
PB_Password.Password = string.Empty;
chk_Effect.IsChecked = false;

9.在取消时,我们还要给提示,因为WPF不像MVC一样会保存记录,所以我们要提示取消之后将不会保留数据

 MessageBoxResult dr = MessageBox.Show("退出界面数据将不保留。", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Information);//弹出确定对话框
 if(dr==MessageBoxResult.OK)//如果点了确定按钮
     {
          //关闭窗口
          this.Close();
      }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值