Private Sub ForgotPasswordPage1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim table2 As New DataTable
Dim cmd As New MySqlCommand
Dim cmd1 As New MySqlCommand
Dim da As New MySqlDataAdapter
Dim da2 As New MySqlDataAdapter
Dim con = New MySqlConnection
con.ConnectionString = "server=127.0.0.1;userid=root;password=;database=pharma"
Try
'we open Connection
con.Open()
With cmd
.Connection = con
.CommandText = "SELECT `security_question_1` from pharma.account_admin where `u_name`='" & Login.u_name.Text & "';"
End With
'declare dt as new datatable
Dim dt As New DataTable
Dim dt2 As New DataTable
With sq
da.SelectCommand = cmd
'it fills the da values into dt
da.Fill(dt)
'dt provides the data surce of combobox
.DataSource = dt
'specify the what to display
.DisplayMember = "security_question_1"
'and the value
.ValueMember = "security_question_1"
End With
With cmd1
.Connection = con
.CommandText = "SELECT `security_question_2` from pharma.account_admin where `u_name`='" & Login.u_name.Text & "';"
End With
With sq
da.SelectCommand = cmd1
'it fills the da values into dt
da.Fill(dt)
'dt provides the data surce of combobox
.DataSource = dt
'specify the what to display
.DisplayMember = "security_question_2"
'and the value
.ValueMember = "security_question_2"
End With
Catch ex As Exception
这是我的代码。我想要安全问题1和安全问题2,这是我的数据库中的两个不同的字段,可以插入组合框中。
答案 0 :(得分:0)
我的代码中最重要的是使用参数。此更改将有助于保护您的数据库免受恶意输入。我还使用了安全问题1和2的别名。(select命令中的As Question。)仔细检查用户名的数据类型。我猜到varchar但它可能是其他东西。相应地更改MySalDbType。使用DataTable.Load将第二个查询中的记录添加到数据表中。实际上,我认为这两个选择可以作为联盟运行。如果您只回到2个问题,那么我会在一个查询中选择两个问题,使用datareader并手动将值添加到组合框中。 Using / End Using语句确保实现IDisosable的对象可以尽快释放其非托管资源。
Using cmd As New MySqlCommand
Using da As New MySqlDataAdapter
Using con = New MySqlConnection("server=127.0.0.1;userid=root;password=;database=pharma")
Try
With cmd
.Connection = con
.CommandType = CommandType.Text
.CommandText = "SELECT `security_question_1` As Question from pharma.account_admin where `u_name`= @UserName;"
.Parameters.Add("@UserName", MySqlDbType.VarChar).Value = Login.u_name.Text
End With
Using dt As New DataTable
da.SelectCommand = cmd
da.Fill(dt)
cmd.CommandText = "SELECT `security_question_2` As Question from pharma.account_admin where `u_name`= @UserName;"
con.Open()
Using dr As MySqlDataReader = cmd.ExecuteReader
dt.Load(dr)
con.Close()
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Question"
ComboBox1.ValueMember = "Question"
End Using
End Using
Catch ex As Exception
End Try
End Using
End Using
End Using