wpf(新增查改)基础知识汇总

开发工具与关键技术:wpf
作者:龙文浩
撰写时间:2019年5月24日

查询员工部门:
在这里插入图片描述
A, 去数据库查询 部门名称和部门ID :
B,考虑数据库设计的特殊性:
在这里插入图片描述
只有当attribute_gather_id = 12 时才是部门;
然后我们要查询 部门名称和部门ID
在这里插入图片描述
然后去到服务器写代码:
写法是固定的,可以直接复制:
//下拉框 员工部门
[OperationContract]
public DataSet onload_SelectPost()
{
SqlParameter[] sqlParameter = {
new SqlParameter("@type",SqlDbType.Char)
};
sqlParameter[0].Value = “(数据库中@type的名称)”;
DataTable dataTable = dalMethod.QueryDataTable(“数据库中存储过程的名称”, sqlParameter);
DataSet dataSet = new DataSet();
dataSet.Tables.Add(dataTable);
return dataSet;
}
配置好了服务之后就来到客户端:
添加服务,
找到页面的部门下拉框的 x:Name值:
在这里插入图片描述
然后,托管代码就下面固定写法:
//下拉框 员工部门
DataTable dbPost = dbs.onload_SelectPost().Tables[0];//固定写法
cbo_Post.ItemsSource = dbPost.DefaultView;//固定写法
cbo_Post.DisplayMemberPath = “Post_name”;//数据库中部门名称
cbo_Post.SelectedValuePath = “IDPost_id”;//数据库中的部门ID
第一、 表格查询
查询员工账户管理:
在这里插入图片描述
A,先写数据库的查询员工账户代码:
在这里插入图片描述
B,然后写服务器上面的代码:
也是固定写法:
DALPublic.DALMethod dALMethod = new DALPublic.DALMethod();
//表格查询
[OperationContract]
public DataSet onload_Select_SystemInformation() {
SqlParameter[] sqlParameter = {
new SqlParameter("@type",SqlDbType.NChar)
};
sqlParameter[0].Value = “(数据库中@type的名称)”;
DataTable dateTable = dALMethod.QueryDataTable("(存储过程名称)", sqlParameter);
DataSet dataSet = new DataSet();
dataSet.Tables.Add(dateTable);
return dataSet;
}
C,配置好服务,要先更新服务,然后实例化服务;
注意表格中的字段必须跟数据库中的一模一样:
在这里插入图片描述
然后,托管代码就一两行,是固定写法:
DataTable dataTable = dbs.onload_Select_SystemInformation().Tables[0];//调用服务器
dgAccountManage.ItemsSource = dataTable.DefaultView;
// (dgAccountManage:表格名称). ItemsSource = dataTable. DefaultView;
表格名称如下图:
在这里插入图片描述
第二、 新增数据
在这里插入图片描述
新增数据:要获取下拉框、文本框、checked。这三种的值:
下拉框获取方式:下拉框的Name值.SelectedValue
文本框获取方式: 文本框的Name值.Text
密码框获取方式:密码框的Name值. Password
checkBox获取方式:checkBox的Name值.IsChecked
在这里插入图片描述
然后去看sql的新增数据:
在这里插入图片描述
数据库新增的字段跟客户端的字段保持一致;
然后客户端是固定写法:
//新增员工
[OperationContract]
public int aClick_AddStaff(int staff_id, string operator_accounts, string operator_password, bool effective, string note) {
SqlParameter[] sqlParameter = {
new SqlParameter("@type",SqlDbType.Char),
new SqlParameter("@staff_id",SqlDbType.Int),
new SqlParameter("@operator_accounts",SqlDbType.Char),
new SqlParameter("@operator_password",SqlDbType.Char),
new SqlParameter("@effective",SqlDbType.Bit),
new SqlParameter("@note",SqlDbType.Char),
};
sqlParameter[0].Value = “数据库@type的名称”;
sqlParameter[1].Value = staff_id;
sqlParameter[2].Value = operator_accounts;
sqlParameter[3].Value = operator_password;
sqlParameter[4].Value = effective;
sqlParameter[5].Value = note;
int count = dALMethod.UpdateData(“数据库存储过程名称”, sqlParameter);
return count;
}
然后要更新客户端的对应服务:实例化服务之后,调用服务:
int count = dbs.aClick_AddStaff(staff_id, operator_accounts, operator_password, effective, note);
参数要一摸一样;
最后就是给一些提示:
if (count > 0)
{
MessageBoxResult dr = MessageBox.Show(“已保存”, “提示”, MessageBoxButton.OKCancel,MessageBoxImage.Asterisk);
if (dr == MessageBoxResult.OK)
{
this.Close();
}
}
else
{
MessageBoxResult dr = MessageBox.Show(“账号重复”, “提示”, MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
第三、 修改数据
在这里插入图片描述
获取表格的一行数据,传输过去另一个页面,数据绑定到修改页面;
A,获取表格一行数据方式:表格的x:Name名称. CurrentItem
备注:DataRowView 是把object转换成一行数据。
获取并转换方式:(DataRowView)表格的x:Name名称.CurrentItem = 一行数据
B,传输数据过去另一个页面方式:
先实例化页面,并且把一行数据放进去末尾括号中;
接收数据的页面名称 dataTable=
new接收数据的页面名称((DataRowView) 表格的x:Name名称. CurrentItem);
然后去到接收数据的页面的托管代码:
在构造函数里接收一行数据:
声明一个变量在外面,然后接收数据,数据就传输到外面;
DataRowView dataRowView2;
public 接收数据的页面名称(DataRowView dataRowView)
{
InitializeComponent();
dataRowView2 = dataRowView;
}
把数据绑定到修改页面的控件里面:
文本框的绑定方式:
文本框的x:Name值.Text = (dataRowView2.Row[“对应的值”]).ToString();
txt_Account.Text = (dataRowView2.Row[“operator_accounts”]).ToString();
密码框的绑定方式:
密码框x:Name值.Password = (dataRowView2.Row[“operator_password”]).ToString();
PB_Password.Password = (dataRowView2.Row[“operator_password”]).ToString();
下拉框的绑定方式:
下拉框的x:Name值.SelectedValue = Convert.ToInt32(dataRowView2.Row[“staff_id”]);
cbo_Name.SelectedValue = Convert.ToInt32(dataRowView2.Row[“staff_id”]);
checkBox的绑定方式:
checkBox的x:Name值.IsChecked = true/false;
if (dataRowView2.Row[“effective”].ToString() == “有效”)
{
chk_Effect.IsChecked = true;

        }
        else
        {
            chk_Effect.IsChecked = false;
        }

C,数据修改:
先获取页面修改后的数据,再存储进去数据库;
int IdStaff_id = Convert.ToInt32(cbo_Name.SelectedValue);//下拉框
string strOperator_accounts = txt_Account.Text.Trim();//文本框
string strOperator_password = PB_Password.Password.Trim();//密码框
bool blEffective = (bool)chk_Effect.IsChecked;//checkBox
string strNote = txt_Note.Text.ToString();//文本框
然后我们还要获取一个主键,dataRowView2.Row[“operator_id”]
int operator_id = Convert.ToInt32(dataRowView2.Row[“operator_id”]);

接着就去数据库写修改语句:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
调用服务之前,记得更新一下服务:
//调用服务器语句
int Count = dbs.Click_UpdateStaff(staff_id, operator_accounts,
operator_password, effective, note, operator_id);

最后进行判断,输出提示即可:
if (Count > 0)
{
MessageBoxResult result = MessageBox.Show(“已修改”, “提示”, MessageBoxButton.OK, MessageBoxImage.Asterisk);
if (result == MessageBoxResult.OK)
{
this.Close();
}
}
else
{
MessageBoxResult result = MessageBox.Show(“未修改”, “提示”, MessageBoxButton.OK, MessageBoxImage.Error);
}

删除数据
在这里插入图片描述
获取选中行数据,提示是否删除,调用服务器
获取选中行数据方式:(DataRowView)表格的x:Name值.CurrentItem
提示是否删除方式:
MessageBoxResult result = MessageBox.Show(“是否删除?”, “提示”, MessageBoxButton.OKCancel, MessageBoxImage.Question);
使用MessageBoxResult接收点击后的值,然后判断用户点击的是不是OK
if (result == MessageBoxResult.OK){
当点击的是OK的时候,就获取选中行数据:
获取表的主键ID,判断主键ID是否存在;
Int operator_id =
Convert.ToInt32(((DataRowView)dataRowView2.CurrentItem).Row[“operator_id”]);

}
在这里插入图片描述
再看sql:
在这里插入图片描述
然后服务器是固定写法:
//删除员工
[OperationContract]
public int Click_DeleteStaff(int operator_id) {
SqlParameter[] sqlParameter =
{
new SqlParameter("@type",SqlDbType.Char),
new SqlParameter("@operator_id",SqlDbType.Int)
};
sqlParameter[0].Value = “Click_DeleteStaff”;
sqlParameter[1].Value = operator_id;
int Count = dALMethod.UpdateData(“UC_StaffAccountManage”, sqlParameter);
return Count;
}
然后更新服务,调用方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值