use*_*784 2 mysql vb.net string double type-conversion
我正在尝试在VB中显示来自数据库的登录用户余额.但是,一旦我单击"检查余额"按钮,就会产生错误Conversion from string "Your balance is " to type 'Double' is not valid.
我尝试过将它从字符串转换为double的不同方法,我想也许是因为我将m_decBalance声明为小数,但这并没有改变任何东西.谁能帮我?这是我的代码:
Imports MySql.Data
Imports MySql.Data.MySqlClient
Public Class Form1
Dim dbCon As MySqlConnection
Dim strQuery As String = ""
Dim SQLcmd As MySqlCommand
Dim DataReader As MySqlDataReader
Private m_strPass As String
Private m_decBalance As Decimal
Private m_strName As String
Private m_strUserPass As String
Private m_strCardNumber As String
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
'Assign users guessed password to variable
m_strUserPass = txtPass.Text
'Invoke
RetrieveAccountInformation()
' determine if Password is correct or not
If m_strUserPass = m_strPass Then
lblWelcome.Visible = True
lblWelcome.Text = "Welcome" + " " + m_strName
txtPass.Enabled = False
btnBalance.Enabled = True
Else
' indicate that incorrect password was provided
lblWelcome.Visible = True
lblWelcome.Text = "Sorry, Password is incorrect." _
& "Please retry ."
' clear user's previous PIN entry
m_strUserPass = ""
End If
txtPass.Clear() ' clear TextBox
End Sub
' load application Form
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Prepare connection and query
Try
dbCon = New MySqlConnection("Server=localhost;Database=test;Uid=root;Pwd=mysql")
strQuery = "SELECT CardNumber " &
"FROM Account"
SQLcmd = New MySqlCommand(strQuery, dbCon)
'Open the connection
dbCon.Open()
' create database reader to read information from database
DataReader = SQLcmd.ExecuteReader
' fill ComboBox with account numbers
While DataReader.Read()
cboAccountNumbers.Items.Add(DataReader("CardNumber"))
End While
'Close the connection
DataReader.Close()
dbCon.Close()
Catch ex As Exception
'Output error message to user with explaination of error
MsgBox("Failure to communicate" & vbCrLf & vbCrLf & ex.Message)
End Try
End Sub
' invoke when user provides account number
Private Sub RetrieveAccountInformation()
' specify account number of record from which data will be retrieved
dbCon = New MySqlConnection("Server=localhost;Database=test;Uid=root;Pwd=mysql")
strQuery = "SELECT Name, Balance, Password " &
"FROM Account WHERE CardNumber='" & Val(cboAccountNumbers.Text) & "' "
SQLcmd = New MySqlCommand(strQuery, dbCon)
dbCon.Open() ' open database connection
' create database reader to read information from database
DataReader = SQLcmd.ExecuteReader
DataReader.Read() ' open data reader connection
' retrieve Password number, balance amount and name information from database
m_strPass = Convert.ToString(DataReader("Password"))
m_decBalance = Convert.ToString(DataReader("Balance"))
m_strName = Convert.ToString(DataReader("Name"))
DataReader.Close() ' close data reader connection
dbCon.Close() ' close database connection
End Sub ' RetrieveAccountInformation
Private Sub btnBalance_Click(sender As Object, e As EventArgs) Handles btnBalance.Click
'Retrieve their account information
RetrieveAccountInformation()
Try
MsgBox("You balance is " + " " + m_decBalance)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
问题是你要加入一个字符串+.你需要更换+用&.我还建议添加.ToString到您的m_decBalance,因为这将告诉编译器将其m_decBalance视为一个string,如下所示:
MsgBox("You balance is " & " " & m_decBalance.ToString)
Run Code Online (Sandbox Code Playgroud)
您收到错误的原因是编译器在与数字一起+使用时尝试将字符串转换为数值.例如,以下内容将显示一个值为的消息框10:
MsgBox("5 " + " " + 5)
Run Code Online (Sandbox Code Playgroud)
和
Dim Val As Integer = "20 " + 15
Run Code Online (Sandbox Code Playgroud)
将导致Val存在35
当你想连接字符串时,我建议使用&,因为这告诉编译器你不希望将字符串转换为数字,而是希望将它们作为字符串连接起来.
我还建议使用Option Strict On,因为这将有助于防止这样的错误发生,因为它阻止你重新编译,如果你有任何implicit conversions(编译器必须猜测你想要转换为哪种类型)