帮助中新增用户代码非常复杂。如下:
Dim UserName As String = e.Form.Controls("UserName").Value
Dim UserGroup As String = e.Form.Controls("UserGroup").Value
Dim PassWord As String = e.Form.Controls("PassWord").Value
Dim cmd As New SQLCommand
cmd.ConnectionName = "database"
If UserName = "" OrElse UserGroup = "" Then
Messagebox.show("请输入用户名和用户分组!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Return
End If
cmd.CommandText = "Select Count(*) Fro m {Users} Where Name = '" & UserName & "'"
If cmd.ExecuteScalar > 0 Then
Messagebox.show("增加用户失败,已经能存在同名用户!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Return
End If
cmd.CommandText = "Insert Into {Users} ([Name],[Group],[Password]) Values ('"
cmd.CommandText = cmd.CommandText & UserName & "','" & UserGroup & "','" & Password & "')"
If cmd.ExecuteNonQuery = 1 Then '返回1表示增加成功
If Forms("用户管理").Opened Then
With Forms("用户管理").Controls("ListBox1")
.Items.Add(UserName)
.SelectedIndex = .Items.Count -1
End With
End If
e.Form.Close()
End If
————————————————————————————————————————————————
我能否修改成下面这样的,思路就是判断是否输入了数据,是否有同名的,如果没有,就保存到新增的ROW中去,然后关闭窗口,我不明白帮助中为什么要写这么复杂。
Dim UserName As String = e.Form.Controls("UserName").Value
Dim UserGroup As String = e.Form.Controls("UserGroup").Value
Dim cmd As New SQLCommand
cmd.C
If UserName = "" OrElse UserGroup = "" Then
Messagebox.show("请输入用户名和用户分组!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Return
End If
cmd.CommandText = "Select Count(*) Fro m {Users} Where Name = '" & UserName & "'"
If cmd.ExecuteScalar > 0 Then
Messagebox.show("增加用户失败,已经能存在同名用户!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Return
Else
Tables("users").AddNew
DataTables("users").Save
e.Form.Close
End If