繁体   English   中英

为什么我在连接 mysql 时在 Vb.net 中收到此错误“从字符串转换为类型 'Double' 无效”?

[英]Why I get this error in Vb.net while connecting mysql “Conversion from string to type ‘Double’ is not valid”?

我分别尝试了这个等式。 有效。 但是我加入后,我得到了转换的错误。 我的目标是获取 MySql 数据到 vb.net 所以我可以检查一些值并开发项目。 我必须在一周内完成这个项目,我不知道如何完成这个。 如果这看起来很容易,请原谅我。

Imports MySql.Data.MySqlClient
Public Class Form3
    Dim conn As MySqlConnection
    Dim command As MySqlCommand
    Dim cmd As MySqlCommand
    Dim Da As New MySqlDataAdapter
    Dim ds As New DataSet

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'To check whether the date is same
        TextBox1.Text = System.DateTime.Now.ToString(("MM/dd/yyyy"))

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        conn = New MySqlConnection
        conn.ConnectionString = "server=localhost;user=root;password=1234;database=attendance"
        Dim reader As MySqlDataReader
        Try
            ds.Clear()
            conn.Open()
            'Checking Subject Now
            cmd = New MySqlCommand("select Subject_Name from dateverification", conn)
            Da = New MySqlDataAdapter(cmd)
            Da.Fill(ds, "dateverification")
            TextBox2.Text = ds.Tables(0).Rows(0).Item(0)

            'Checking Todays Date
            cmd = New MySqlCommand("select Today_Date from dateverification", conn)
            Da = New MySqlDataAdapter(cmd)
            Da.Fill(ds, "dateverification")
            Label1.Text = ds.Tables(0).Rows(0).Item(0)

            'Checking Count1
            cmd = New MySqlCommand("select Count1 from dateverification", conn)
            Da = New MySqlDataAdapter(cmd)
            Da.Fill(ds, "dateverification")
            Label2.Text = ds.Tables(0).Rows(0).Item(0)

            'Checking Count2
            cmd = New MySqlCommand("select Count2 from dateverification", conn)
            Da = New MySqlDataAdapter(cmd)
            Da.Fill(ds, "dateverification")
            Label3.Text = ds.Tables(0).Rows(0).Item(0)

            'If the days are Different, Total days will be counted and Date will be updated
            If Label1.Text <> TextBox1.Text Then
                Label1.Text = System.DateTime.Now.ToString(("yyyy-MM-dd"))
                Label2.Text = Label2.Text + 1
                Dim query1 As String
                query1 = "UPDATE attendance.dateverification SET Today_Date = '" & Label1.Text & "' , Count1 = '" & Label2.Text & "' WHERE Subject_Name = '" & TextBox3.Text & "'; "
                command = New MySqlCommand(query1, conn)
                reader = command.ExecuteReader
                MessageBox.Show("Welcome to New Day")

            Else
            'If the date are equal, then the number of counts which register wasopen in same day will be increased
                Label3.Text = Label3.Text + 1
                Dim query1 As String
                query1 = "UPDATE attendance.dateverification SET Count2 = '" & Label3.Text & "' WHERE Subject_Name = '" & TextBox3.Text & "'; "
                command = New MySqlCommand(query1, conn)
                reader = command.ExecuteReader
                MessageBox.Show("You are still on the same day")

            End If

            Dim query As String
            query = "UPDATE attendance.dateverification SET Subject_selected = '" & TextBox3.Text & "' WHERE Subject_Name = '" & TextBox3.Text & "'; "
            command = New MySqlCommand(query, conn)
            reader = command.ExecuteReader
            conn.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn.Dispose()

        End Try

错误可能是因为此代码并不总是合法的:

Label3.Text = Label3.Text + 1

Label3.Text是一个字符串。 根据编译器选项,您不能只期望编译器为您将其转换为数字。 这些选项可以帮助您,您应该使用它们。

继续。

在 MySql 上不是 100% 确定,但在 Sql 服务器中,您可以确定您可以将所有这些作为数据库中的一个连接命令作业来完成。 这将表现得更好,并帮助我大大简化了代码。

特别注意我是如何使用参数的。 使用字符串连接将参数值放入查询中是绝对不行的。 即使对于学习或概念验证代码,这也太重要了。

Option Strict On
Option Infer On

Imports System
Imports MySql.Data.MySqlClient

Public Class Form3

    'Don't try to keep the connection/command objects at the form level!
    'Do keep the connection string here:
    Private ConnectionString As String = "server=localhost;user=root;password=1234;database=attendance"

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Text = Today.ToString("MM/dd/yyyy")
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim conn As SqlConnection

        'I put two statements in one command string,
        ' and the CASE expressions let me get all of the updates into one statement
        Dim sql As String = 
"UPDATE attendance.dateverification 
    SET Count1 = CASE WHEN Today_Date <> current_date THEN Count1 + 1 ELSE Count1 END,
        Count2 = CASE WHEN Today_Date = current_date THEN Count2 + 1 ELSE Count2 END,   
        Today_Date = current_date,
        Subject_selected = @subject 
WHERE Subject_Name = @subject;

SELECT Subject_Name, Today_Date, Count1, Count2 
FROM dateverification"
' Also note: this string could be a constant if we wanted to!

        Try
            conn = New MySqlConnection(ConnectionString)
            Dim cmd As New MySqlCommand(sql, conn)
            'Use actual type and length here
            cmd.Parameters.Add("@subject", MySqlDbType.VarString, 50).Value = TextBox3.Text

            conn.Open()
            Dim rdr As MySqlDataReader = cmd.ExecuteReader()
            rdr.Read()

            TextBox2.Text = rdr("Subject_Name").ToString()
            Label1.Text = rdr("Today_Date").ToString()
            Label2.Text = rdr("Count1").ToString()
            Label3.Text = rdr("Count2").ToString()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally 
            conn.Dispose()
        End Try
    End Sub
End Class

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM