gpt4 book ai didi

mysql - vb.net,mysql插入日期

转载 作者:行者123 更新时间:2023-11-29 12:21:44 26 4
gpt4 key购买 nike

嗨,我尝试将 vb.net 中的日期插入到我的 sql 数据库中,结果一直显示为 00-00-00 我使用日期时间选择器来获取格式化为短的日期

Public Sub NewAppointment()

SQLCommand.Connection = SQLconnection
SQLCommand.CommandText = "INSERT INTO " & _
appointment(TattooID,Date,Time,Length,Deposit,Cost) VALUES"& _
('" & _Tattoo.ID & "','"& _
" & AppDate.Date & "','" & _
" & AppTime & "','" & _
" & AppLength.ToString & "','" & _
" & AppDespoit & "','" & AppCost & "');"


SQLCommand.CommandType = CommandType.TableDirect

Try
SQLconnection.Open()
SQLCommand.ExecuteNonQuery()
SQLconnection.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

SQLconnection.Dispose()

结束子

最佳答案

像往常一样,这些问题(以及其他问题)可以通过参数化查询完全避免

这是一个示例,您需要为每个参数修复正确的数据类型 (MySqlDbType),以匹配数据表字段的数据类型

SQLCommand.Connection = SQLconnection
SQLCommand.CommandText = "INSERT INTO " & _
" appointment(TattooID,Date,Time,Length,Deposit,Cost) VALUES "& _
" (@id, @appDate, @AppTime, @AppLength,@AppDespoit,@AppCost);"
SQLCommand.Parameters.Add("@id", MySqlDbType.VarChar).Value = _Tattoo.ID
SQLCommand.Parameters.Add("@AppDate", MySqlDbType.Date).Value = AppDate.Date
SQLCommand.Parameters.Add("@AppTime", MySqlDbType.Date).Value = AppTime
SQLCommand.Parameters.Add("@AppLength", MySqlDbType.VarChar).Value = AppLength
SQLCommand.Parameters.Add("@AppDespoit", MySqlDbType.VarChar).Value = AppDespoit
SQLCommand.Parameters.Add("@AppCost", MySqlDbType.VarChar).Value = AppCost

现在,为日期字段获取正确值的作业将传递给数据库引擎,该引擎接收日期类型的参数,并知道如何从参数中提取值并将其存储在相关字段中。

请注意您的查询现在更具可读性,并且作为额外的好处,您可以避免任何可能的 Sql Injection

关于mysql - vb.net,mysql插入日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28917267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com