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();
}