【機房重構】——使用者管理
阿新 • • 發佈:2019-01-04
【前言】
如何進行使用者管理,無外乎就是對其進行基本的增刪改查,但是又要在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 (' [email protected]+','[email protected]+','[email protected]+','[email protected]+')'
END
IF(@jianzhi=3)
begin
SET @sql='UPDATE [dbo].[User] SET PassWord='[email protected]+',Level='[email protected]+',RegistrarID='[email protected]+' WHERE UserName='[email protected] +''
END
IF(@jianzhi=4)
begin
SET @sql='DELETE FROM [dbo].[User] WHERE UserName='[email protected]+''
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);