先在服务端创建一个方法,把数据从数据库获取。
实例化数据层
实例化对象数组(序列化参数)
- SqlParameter[] mySqlParameters = {
定义传递参数,以及传递参数的类型
new SqlParameter("@type",SqlDbType.NChar),
};
mySqlParameters[0].Value = “Window_Loaded_SelectStaff”;获取执行的存储过程名称
DataSet ds = myDALMethod.QueryDataSet(“UC_StaffAccountManage”, mySqlParameters);
返回值
return ds;
新增里面有个选择的值,所以我要绑定一下下拉框
2. DataTable dt = myClient.Window_Loaded_SelectStaff().Tables[0];
获取数据
cbo_Name.ItemsSource = dt.DefaultView;
绑定数据源
cbo_Name.SelectedValuePath = “staff_id”;
id选中值
cbo_Name.DisplayMemberPath = “name”;
name显示值
-
保存填写的数据到数据库并判断是否重复,不重复可新增。
用try捕捉错误
try
{
判断页面数据是否为空
if (cbo_Name.SelectedValue.ToString() != “” && txt_Account.Text.Trim() != “” && PB_Password.Password.Trim() != “”)
{
获取页面控件数据
获取页面数据
int intID = Convert.ToInt32(cbo_Name.SelectedValue);
string strAccounts = txt_Account.Text.Trim();
string strPassword = PB_Password.Password.Trim();
bool blEffective = (bool)chk_Effect.IsChecked;
string strNote = txt_Note.Text.Trim();
执行新增
int intCount = myClient.btn_Save_Click_InsertStaffAccountManage(intID, strAccounts, strPassword, blEffective, strNote);
//(4)提示
if (intCount > 0)
{
MessageBoxResult dr = MessageBox.Show(“您新注册了一个账号!”, “系统提示”, MessageBoxButton.OKCancel,
MessageBoxImage.Asterisk); //弹出确定对话框
if (dr == MessageBoxResult.OK) //如果点了确定按钮
{
//关闭窗口
this.Close();
}} else if (intCount == -1) {
如果用户填写的数据里面数据库已经存在则出现提示框提示用户账号重复
MessageBox.Show(“账号重复!”, “系统提示”, MessageBoxButton.OKCancel,
MessageBoxImage.Exclamation); //弹出确定对话框
}
}
else
{
如果用户没把页面数据填写完整就会提示请把页面数据填写完整
MessageBox.Show(“请把页面数据填写完整!”, “系统提示”, MessageBoxButton.OK,
MessageBoxImage.Warning); //弹出确定对话框
}
}
catch (Exception)
{
throw;
}