You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中如何获取当前登录用户标识以填充个人信息窗体

解决方案:传递当前登录用户ID填充个人信息窗体

我来帮你解决这个问题——核心就是在登录成功后保存当前用户的PATIENT_ID,然后在个人信息窗体中用这个ID作为查询参数。下面是具体的实现步骤:


1. 先存储当前登录用户的ID

首先需要修改你的登录窗体代码,在验证成功后提取并保存用户的PATIENT_ID。这里推荐两种常用方式:

方式一:使用静态类存储全局用户信息(简单易用)

先创建一个静态类来保存当前登录用户的核心信息,这样所有窗体都能直接访问:

Public Class CurrentUser
    ' 存储当前用户的PATIENT_ID,类型根据你数据库的字段调整(比如Integer或String)
    Public Shared Property PatientId As Integer
    ' 可以按需添加其他属性,比如邮箱、姓名等
End Class

然后修改登录按钮的代码,在登录成功时赋值:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Using connection As New SqlConnection("server=DESKTOP-PL1ATUA\DMV;Database=EHR;Integrated Security=True")
        Dim command As New SqlCommand("select * from patient where user_email = @email and user_pass = @pass", connection)
        command.Parameters.Add("@email", SqlDbType.VarChar).Value = email.Text
        command.Parameters.Add("@pass", SqlDbType.VarChar).Value = pass.Text

        Dim adapter As New SqlDataAdapter(command)
        Dim table As New DataTable()
        adapter.Fill(table)

        If table.Rows.Count() <= 0 Then
            MessageBox.Show("Username or Password are Invalid")
        Else
            MessageBox.Show("Login Successful")
            ' 提取当前用户的PATIENT_ID并保存到静态类
            CurrentUser.PatientId = Convert.ToInt32(table.Rows(0)("PATIENT_ID"))
            dashboard.Show()
            Me.Close() ' 关闭登录窗体
        End If
    End Using
End Sub

方式二:通过构造函数传递ID(更符合封装性)

如果你不想用全局变量,可以在打开个人信息窗体时通过构造函数传递ID:
首先修改PersonalInfo窗体的构造函数:

Private _currentPatientId As Integer

' 自定义构造函数,接收用户ID
Public Sub New(patientId As Integer)
    InitializeComponent()
    _currentPatientId = patientId
End Sub

之后在需要打开PersonalInfo的地方(比如dashboard窗体),这样调用:

' 假设你已经在登录时保存了PatientId到CurrentUser类
Dim personalForm As New PersonalInfo(CurrentUser.PatientId)
personalForm.Show()

2. 在个人信息窗体中使用ID查询数据

现在修改PersonalInfo_Load事件的代码,用保存好的ID替换#idontknow,同时优化资源释放(用Using语句自动关闭连接和阅读器):

对应方式一的代码:

Private Sub PersonalInfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Using connection As New SqlConnection("server=DESKTOP-PL1ATUA\DMV;Database=EHR;Integrated Security=True")
        connection.Open()
        Dim sqlcmd As New SqlCommand("SELECT * FROM PATIENT WHERE PATIENT_ID = @id", connection)
        ' 使用静态类中保存的用户ID作为参数
        sqlcmd.Parameters.AddWithValue("@id", CurrentUser.PatientId)

        ' 使用Using自动释放阅读器资源
        Using reader As SqlDataReader = sqlcmd.ExecuteReader()
            ' 因为PATIENT_ID是主键,只会有一条记录,用If代替While更高效
            If reader.Read() Then
                fname.Text = reader("PATIENT_FNAME").ToString()
                ComboBox1.Text = reader("patient_gender").ToString()
                TextBox4.Text = reader("patient_street").ToString()
                TextBox5.Text = reader("patient_city").ToString()
                TextBox6.Text = reader("patient_state").ToString()
                TextBox7.Text = reader("patient_zip").ToString()
                TextBox8.Text = reader("patient_phone").ToString()
                email.Text = reader("user_email").ToString()
            End If
        End Using
    End Using
End Sub

对应方式二的代码:

只需要把参数部分改成用_currentPatientId即可:

sqlcmd.Parameters.AddWithValue("@id", _currentPatientId)

额外注意事项

  • 确保PATIENT_ID的数据类型和你代码中的类型一致(比如数据库是int,代码中用Integer),避免类型转换错误。
  • 始终使用参数化查询(你已经在做了,很棒!),防止SQL注入攻击。
  • 用Using语句包裹数据库连接、命令和阅读器,确保资源被正确释放,避免内存泄漏。

内容的提问来源于stack exchange,提问作者Aundrea Vickers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:03:58