{“mysqltimespan只能序列化timespan对象”}使用vb.net

xvw2m8pv  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(363)

我有form2在我的表中添加了一个新数据,然后出现了以下错误:

{"Only TimeSpan objects can be serialized by MySqlTimeSpan"}

当我删除这部分代码时,它可以工作:

cm.Parameters.Add(New MySqlParameter("@Time_Operation",  MySqlDbType.Time)).Value = Time_OperationTextBox.Text
    cm.Parameters.Add(New MySqlParameter("@Start_Time", MySqlDbType.Time)).Value = Start_TimeTextBox.Text
    cm.Parameters.Add(New MySqlParameter("@End_Time", MySqlDbType.Time)).Value = End_TimeTextBox.Text

这意味着datetime运行良好,但这三个参数的问题,我把它们作为

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

    Dim str As String = "Server=localhost;Port=3306;Database=springdb;Uid=root;Pwd=2342"

    Using con As New MySqlConnection(str)

        Dim query As String = "Insert Into springdata (full_name,rfid_tag,spring_size,operation_time,Date_Operation,
                                    Time_Operation,Start_Time,End_Time,Qty_Needed) values
                                    (@full_name, @rfid_tag, @spring_size, @operation_time,
                                    @Date_Operation, @Time_Operation, @Start_Time, @End_Time, @Qty_Needed) 
                                      " 'Note:TextBox3 is the RFID number come from RFID arduino

        Dim cm As New MySqlCommand(query, con)
        cm.Parameters.Add(New MySqlParameter("@full_name", MySqlDbType.LongText)).Value = TextBox1.Text
        cm.Parameters.Add(New MySqlParameter("@rfid_tag", MySqlDbType.LongText)).Value = TextBox2.Text
        cm.Parameters.Add(New MySqlParameter("@spring_size", MySqlDbType.MediumText)).Value = TextBox3.Text
        cm.Parameters.Add(New MySqlParameter("@operation_time", MySqlDbType.DateTime)).Value = DateTimePicker1.Value
        cm.Parameters.Add(New MySqlParameter("@Date_Operation", MySqlDbType.Date)).Value = DateTimePicker2.Value
        cm.Parameters.Add(New MySqlParameter("@Time_Operation", MySqlDbType.Time)).Value = Time_OperationTextBox.Text
        cm.Parameters.Add(New MySqlParameter("@Start_Time", MySqlDbType.Time)).Value = Start_TimeTextBox.Text
        cm.Parameters.Add(New MySqlParameter("@End_Time", MySqlDbType.Time)).Value = End_TimeTextBox.Text
        cm.Parameters.Add(New MySqlParameter("@Qty_Needed", MySqlDbType.Int32)).Value = TextBox7.Text

        con.Open()

        cm.ExecuteNonQuery()

        MessageBox.Show("success")
    End Using

End Sub
but5z9lq

but5z9lq1#

我是这样想的:

Dim cm As New MySqlCommand(("Insert Into springdata (full_name,rfid_tag,spring_size,operation_time,Date_Operation,
                                    Time_Operation,Start_Time,End_Time,Qty_Needed )  Values ('" & TextBox1.Text & "', '" & TextBox2.Text & "',
                                    '" & ComboBox1.Text & "', '" & DateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss") & "',
                                     '" & DateTimePicker2.Value.ToString("yyyy-MM-dd") & "', '" & Time_OperationTextBox.Text & "', 
                                      '" & Start_TimeTextBox.Text & "', '" & End_TimeTextBox.Text & "', 
                                      '" & TextBox7.Text & "')"), con)

不幸的是我没能解决它 timespan,parse :-)

ev7lccsx

ev7lccsx2#

错误消息告诉您,您要为参数值提供的是类型 string ,但您的mysql方法需要 TimeSpan -您需要将文本框中输入的字符串作为 TimeSpan ,然后将结果timespan变量传递到mysql参数,而不是字符串。
这个 TimeSpan.Parse() 方法接受字符串作为输入,然后使用它从字符串中读取的值输出timespan对象。

cm.Parameters.Add(New MySqlParameter("@Time_Operation",  MySqlDbType.Time)).Value = TimeSpan.Parse(Time_OperationTextBox.Text)
cm.Parameters.Add(New MySqlParameter("@Start_Time", MySqlDbType.Time)).Value = TimeSpan.Parse(Start_TimeTextBox.Text)
cm.Parameters.Add(New MySqlParameter("@End_Time", MySqlDbType.Time)).Value = TimeSpan.Parse(End_TimeTextBox.Text)

当然,在此之前,您必须确保用户在文本框中输入的内容是timespan代码能够理解的有效格式(请参阅下面的文档链接以获取有关可接受格式的信息)。
文档:https://docs.microsoft.com/en-us/dotnet/api/system.timespan.parse?view=netframework-4.7.2

相关问题