如何将mysql值分配给vb.net中的单个变量?

时间:2014-07-11 11:12:39

标签: mysql vb.net

以下是忘记密码的代码, 我想将mysql查询的值存储在变量中,以便我可以使用变量值将其放入电子邮件中。

Private Sub Button3_Click(sender As Object, e As EventArgs) 
Handles Button3.Click

        Dim p_text As String
        p_text = TextBox1.Text
        MsgBox(p_text)
        Dim i As Integer
        Try

            Dim conn As MySqlConnection
            conn = New MySqlConnection
            conn.ConnectionString ="server=192.168.1.111;userid=betterhomes;
            password=123456;database=betterhomes"

            conn.Open()
            Dim password As String = ""
            Dim sqlquery2 As String = "SELECT bh_password FROM bh_login 
            where job_id = '" & p_text & "';"

            Dim data2 As MySqlDataReader
            Dim adapter2 As New MySqlDataAdapter
            Dim command2 As New MySqlCommand
            command2.CommandText = sqlquery2
            command2.Connection = conn
            adapter2.SelectCommand = command2
            data2 = command2.ExecuteReader

            While data2.HasRows
                password = data2.GetString("bh_password")
                MsgBox(password)
                data2.Close()
            End While

        Catch exError As MySqlException
            '  MsgBox("Error connecting to database! Try again later")
        End Try

    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

尝试这样

 If data2.Read
    password = data2.GetString("bh_password")
    MsgBox(password)
 End if
 data2.Close()

发送电子邮件

Dim mail As New MailMessage()

'set the addresses
mail.From = New MailAddress("xx@xx")
mail.[To].Add("xx@xx")

' //set the content
mail.Subject = "This is an email"
mail.Body = password 

' // set the server
Dim smtp As New SmtpClient("localhost")

' // send the message
Try
    smtp.Send(mail)
    Response.Write("Your Email has been sent sucessfully - Thank You")
Catch exc As Exception
    Response.Write("Send failure: " & exc.ToString())
End Try