【机房重构】——用户管理

【前言】

如何进行用户管理,无外乎就是对其进行基本的增删改查,但是又要在DAL写好几个SQL语句,想想也挺头大的还是果断使用存储过程吧,存储过程的好处又很多,我就不一一例举了,下面进入正题。

 

【正文】

存储过程代码

ALTER PROCEDURE  [dbo].[PROC_User]
	-- Add the parameters for the stored procedure here
	@txtUserName varchar(20),
	@txtPWD varchar(20),
	@txtlevel varchar(20),
	@UserName varchar(20),
	@jianzhi int
AS
	declare @sql varchar(500)
BEGIN
	if(@jianzhi=1)
		begin
		SET @sql='SELECT * FROM [dbo].[User]'
	END
	IF(@jianzhi=2)
		begin
		SET @sql='INSERT INTO [dbo].[User] VALUES ('+@txtUserName+','+@txtPWD+','+@txtlevel+','+@UserName+')'
	END 
	
	IF(@jianzhi=3)
		begin
		SET @sql='UPDATE [dbo].[User] SET PassWord='+@txtPWD+',Level='+@txtlevel+',RegistrarID='+@UserName+' WHERE UserName='+@txtUserName+''
	END
	IF(@jianzhi=4)
		begin
		SET @sql='DELETE FROM [dbo].[User] WHERE UserName='+@txtUserName+''
	
	END
	exec(@sql)

   
END

 UI层代码

//C#中datagridview选中行后textbox显示选中的内容 
        private void dataGridView1_SelectionChanged_1(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectionMode != DataGridViewSelectionMode.FullColumnSelect)
            {
                int index = dataGridView1.CurrentRow.Index; //获取选中行的行号
                txtUserName.Text = dataGridView1.Rows[index].Cells[1].Value.ToString();
                txtPwd.Text = dataGridView1.Rows[index].Cells[2].Value.ToString();
                txtlevel.Text = dataGridView1.Rows[index].Cells[3].Value.ToString();
            };
        }
//判断选中的是否是最后一行
 dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                if (dataGridView1.SelectedRows[0].Index == dataGridView1.Rows.Count - 1)
                {
                    button1.Enabled = true;
                }
                else
                {
                    button1.Enabled = true;
                }
//添加用户
foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    if (dr.Cells[1]. ToString() == txtUserName.Text.Trim())
                    {
                        //不添加行
                        MessageBox.Show("用户名重复,请修改后重试", "温馨提示", 0, MessageBoxIcon.Warning);
                        txtUserName.Text = "";
                        txtUserName.Select(0, 0);
                        return;
                    }
                    
                }
                DataTable flag = facade.User(UserInfo);
                if (flag.Rows.Count!=0)
                {
                    MessageBox.Show("添加成功", "温馨提示", 0, MessageBoxIcon.Warning);
                }
//删除选中的用户
 foreach (DataGridViewRow row in dataGridView1.SelectedRows )
                {
                    UserInfo.jianzhi = 4;
                    UserInfo.txtUserName= row.Cells[1].Value.ToString();
                    DataTable flag = facade.User(UserInfo);
                    DataRowView drv = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;
                    drv.Delete();
                }
                MessageBox.Show("删除成功!", "温馨提示", 0, MessageBoxIcon.Warning);

【总结】

在完成这些基本功能后,还是有一些点未能解决,比如说添加用户选中空白行以后才能添加,存在一些小问题,欢迎大家能够指正。

 

猜你喜欢

转载自blog.csdn.net/damishidai15/article/details/85040818