如何使用 VB.NET 在 MySql 中分配空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2390348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To assign null values in MySql using VB.NET
提问by fireBand
I am not sure why VB makes everything such a pain. I have a fields which stores the date and time in the format required to store in MySQL database
我不知道为什么 VB 使一切变得如此痛苦。我有一个字段,它以存储在 MySQL 数据库中所需的格式存储日期和时间
Dim AppDate As String = String.Empty
If Not String.IsNullOrEmpty(Me.AppDate.Text.Trim) Then
AppDate = Format(CDate(Me.AppDate.Text), "yyyy-MM-dd h:mm:ss")
Else
//Need to assign a null value to AppDate
End If
Now I need to assign the AppDate to NUll like DBNull, but I am not able to do it directly. If I change AppDate to Date then I am not getting the required format.
现在我需要像 DBNull 一样将 AppDate 分配给 NUll,但我无法直接执行此操作。如果我将 AppDate 更改为 Date,那么我将无法获得所需的格式。
Any help is appreciated .
任何帮助表示赞赏。
Thanks in Advance.
提前致谢。
采纳答案by Dan
AppDate = Nothing should work, you could also use DateTime.MinValue and update your business logic to treat it appropriately.
AppDate = 没有任何效果,您也可以使用 DateTime.MinValue 并更新您的业务逻辑以适当处理它。
回答by IniTech
AppDate = Nothing
应用日期 = 无
回答by Joel Coehoorn
the date and time in the format required to store in MySQL database
存储在 MySQL 数据库中所需格式的日期和时间
If you're building a datetime string to save to a database, you're doing it all wrong.
如果您正在构建一个日期时间字符串以保存到数据库,那么您就做错了。
Using the MySql Connector/Net, you should be building your query like this:
使用 MySql 连接器/网络,你应该像这样构建你的查询:
Dim sql As String = "UPDATE `MyTable` SET `MyField` = @MyField WHERE `ID` = @MyID"
Using cn As New MySqlconnection("..your connection string here.."), _
cmd As New SqlCommand(sql, cn)
cmd.Parameters.Add("@MyField", MySqlDbType.DateTime).Value = MyDateTimeVar ''# NO FORMATTING NEEDED!
cmd.Parameters.Add("@MyID", MySqlDbType.Int).Value = MyIDIntegerVar
cn.Open()
cmd.ExecuteNonQuery()
End Using
回答by Mojtaba Rezaeian
Mysql allows NULL
values for datetime only if you set ConvertZeroDateTime=True
option in connectionstring. This value is set to false
by default.
NULL
仅当您在 connectionstring 中设置ConvertZeroDateTime=True
选项时,Mysql 才允许日期时间值。该值false
默认设置为。
MySql DocumentationDescribes:
MySql 文档描述:
ConvertZeroDateTime(Default: False):
ConvertZeroDateTime(默认: False) :
True to have MySqlDataReader.GetValue() and MySqlDataReader.GetDateTime() return DateTime.MinValue for date or datetime columns that have disallowed values.
让 MySqlDataReader.GetValue() 和 MySqlDataReader.GetDateTime() 为具有不允许值的日期或日期时间列返回 DateTime.MinValue 是真的。
AllowZeroDateTime(Default: False):
AllowZeroDateTime(默认: False) :
If set to True, MySqlDataReader.GetValue() returns a MySqlDateTime object for date or datetime columns that have disallowed values, such as zero datetime values, and a System.DateTime object for valid values. If set to False (the default setting) it causes a System.DateTime object to be returned for all valid values and an exception to be thrown for disallowed values, such as zero datetime values.
如果设置为 True,则 MySqlDataReader.GetValue() 为具有不允许值(例如零日期时间值)的日期或日期时间列返回一个 MySqlDateTime 对象,并为有效值返回一个 System.DateTime 对象。如果设置为 False(默认设置),它会导致为所有有效值返回 System.DateTime 对象,并为不允许的值(例如零日期时间值)抛出异常。
回答by Wesam
this actully the only way it worked for me , just use System.Data.SqlTypes.Sql Data Type .Null for example when an sql field is type Char(3) , then use System.Data.SqlTypes.SqlChars.Null , string.empty or nothing didn't work for me
这实际上是它对我有用的唯一方法,例如,当 sql 字段的类型为 Char(3) 时,只需使用 System.Data.SqlTypes.Sql 数据类型 .Null ,然后使用 System.Data.SqlTypes.SqlChars.Null ,字符串。空的或什么都没有对我有用
Dim SQLCom = New SqlCommand
Dim SQLCon As New SqlClient.SqlConnection(_ConnString)
Dim _Param5 As New SqlParameter("@FieldName", SqlDbType.Char, 3)
With SQLCom
.Connection = SQLCon
.CommandText = "StoredProcedure_name"
.CommandType = CommandType.StoredProcedure
.Parameters.Add(_Parameter5)
End With
_Parameter5.Value = System.Data.SqlTypes.SqlChars.Null
回答by Robert
AppDate = DbNull.Value 'does this work?
回答by M.A. Hanin
The string value returned by the DbNull.ToString()
method is the empty string (""), which may imply that the string representation of DbNull is the empty string.
DbNull.ToString()
方法返回的字符串值为空字符串(""),这可能暗示DbNull的字符串表示是空字符串。