在做机房收费系统.net个人版结账功能的时候,需要通过选择用户ID获取用户姓名。这个功能我用了两步来实现。
首先,要在窗体加载的时候将查询到的用户ID添加到comboUserID中(即用户ID),这一步是在frmCheckOut的load事件中写的。因为只有操作员和管理员才有权限进行注册、充值、退卡操作,所以查询条件为“用户级别不等于一般用户”即可。
D层SQL语句为:
'查询用户
Public Shared Function CheckUserID(ByVal enUser As UserInfoEntity) As DataTable
Dim sqlhelper As New sqlHelper.sqlHelper
Dim cmdtext As String = "select UserID from [T_User] where [Level]<>@Levle"
Dim sqlparas As SqlParameter() = {New SqlParameter("@Levle", "一般用户")}
Dim dtUser As DataTable = sqlhelper.ExecSelect(cmdtext, CommandType.Text, sqlparas)
Return dtUser
End Function
U层代码为:
Private Sub frmCheckOut_Load(sender As Object, e As EventArgs) Handles Me.Load
'实例化实体
Dim enUser As New UserInfoEntity
Dim dtUser As DataTable = UserManageBLL.CheckUserID(enUser)
'将用户全部添加到comboUserID控件中
For k = dtUser.Rows.Count To 1 Step -1
Dim UserID As String = dtUser.Rows(k - 1)(0)
comboUserID.Items.Add(UserID)
Next
txtUserName.Enabled = False
txtUserName.BackColor = Color.White
End Sub
运行结果如下图:
接下来就是通过用户ID的选择关联显示用户姓名,这一功能是在comboUserID的SelectedIndexChanged事件中实现的。
D层代码为:
'通过用户ID查询用户名
Public Shared Function CheckUserName(ByVal enUser As UserInfoEntity) As DataTable
Dim sqlhelper As New sqlHelper.sqlHelper
Dim cmdtext As String = "select UserName from [T_User] where UserID =@UserID"
Dim sqlparas As SqlParameter() = {New SqlParameter("@UserID", enUser.UserID)}
Dim dtUserName As DataTable = sqlhelper.ExecSelect(cmdtext, CommandType.Text, sqlparas)
Return dtUserName
End Function
U层代码为:
Private Sub comboUserID_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboUserID.SelectedIndexChanged
'根据用户ID的选择结果关联控件comboUserName
Dim enUser As New UserInfoEntity
enUser.UserID = Trim(comboUserID.Text)
'调用B层函数
Dim dtUserName As DataTable = UserManageBLL.CheckUserName(enUser)
'显示用户姓名
txtUserName.Text = dtUserName.Rows(0)(0)
End Sub
运行结果为:
其实这个小功能挺简单的,但是刚开始的时候在显示用户ID的时候走了点弯路,任何问题解决了以后都觉得它是简单的,但是那个思考和实践的过程却是别有一番滋味在心头。