English 中文(简体)
How To assign null values in MySql using VB.NET
原标题:

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

 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.

Any help is appreciated .

Thanks in Advance.

最佳回答

AppDate = Nothing should work, you could also use DateTime.MinValue and update your business logic to treat it appropriately.

问题回答

AppDate = Nothing

the date and time in the format required to store in MySQL database

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:

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
AppDate = DbNull.Value  does this work?

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.

Mysql allows NULL values for datetime only if you set ConvertZeroDateTime=True option in connectionstring. This value is set to false by default.


MySql Documentation Describes:

ConvertZeroDateTime (Default: False):

True to have MySqlDataReader.GetValue() and MySqlDataReader.GetDateTime() return DateTime.MinValue for date or datetime columns that have disallowed values.


AllowZeroDateTime (Default: 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.

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

    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




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签