正文

家庭视频监控系统(17)

C#项目开发案例全程实录(第2版) 作者:明日科技


单击“添加”按钮,首先判断“用户名”文本框是否为空,如果为空,则弹出“用户名不能为空”提示信息;否则,判断数据库中是否存在该用户。如果存在,则弹出“该用户已经存在”信息提示;否则,将“用户名”文本框中的用户名存储到数据库中。“添加”按钮的Click事件的代码如下:

例程32 代码位置:光盘\TM\01\VWMS\VWMS\frmSetMonitor.cs

private void btnAdd_Click(object sender, EventArgs e)

{

if (txtName.Text == string.Empty)

{

MessageBox.Show("用户名不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

else

{

//根据用户输入的登录名和密码获取DataSet数据集

ds = dataoperate.getDs("select * from tb_admin where name='" + txtName.Text + "'", "tb_admin");

if (ds.Tables[0].Rows.Count > 0)

{

MessageBox.Show("该用户已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

else

{

//执行添加管理员操作

dataoperate.getCom("insert into tb_admin (name,pwd) values('" + txtName.Text + "','" + txtPwd.Text + "')");

lviewBind(); //重新绑定ListView控件

}

}

}

单击“修改”按钮,首先判断要修改后的“用户名”是否与数据库中原有记录冲突。如果是,弹出“该用户已经存在”信息提示;否则,成功修改选中的用户信息。“修改”按钮的Click事件的代码如下:

例程33 代码位置:光盘\TM\01\VWMS\VWMS\frmSetMonitor.cs

private void btnEdit_Click(object sender, EventArgs e)

{

ds = dataoperate.getDs("select * from tb_admin where name='" + txtName.Text + "'", "tb_admin");

if (ds.Tables[0].Rows.Count > 0)

{

MessageBox.Show("该用户已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

else

{

//执行修改管理员操作

dataoperate.getCom("update tb_admin set name ='" + txtName.Text + "' and pwd ='" + txtPwd.Text + "'");

lviewBind();

}

}

单击“删除”按钮,首先判断选中的用户是不是超级用户,如果是,弹出“该用户是超级用户,不能删除”信息提示;否则,删除选中的用户。“删除”按钮的Click事件的代码如下:

例程34 代码位置:光盘\TM\01\VWMS\VWMS\frmSetMonitor.cs

private void btnDel_Click(object sender, EventArgs e)

{

if (txtName.Text.ToLower() == "tsoft")

{

MessageBox.Show("该用户是超级用户,不能删除!", "警告", MessageBoxButtons.OK, MessageBoxIcon. Warning);

}

else

{

//执行删除管理员操作

dataoperate.getCom("delete from tb_admin where name='" + txtName.Text + "'");

lviewBind();

txtName.Text = lview.Items[0].Text; //在“用户名”文本框中显示超级用户名

}

}

在ListView控件中选择用户时,将该用户的名字显示在“用户名”文本框中,同时清空“密码”文本框。ListView控件的Click事件的代码如下:

例程35 代码位置:光盘\TM\01\VWMS\VWMS\frmSetMonitor.cs

private void lview_Click(object sender, EventArgs e)

{

txtName.Text = lview.SelectedItems[0].Text; //在“用户名”文本框中显示选择的用户

txtPwd.Text = string.Empty;

}


上一章目录下一章

Copyright © 读书网 www.dushu.com 2005-2020, All Rights Reserved.
鄂ICP备15019699号 鄂公网安备 42010302001612号