Skip to content Skip to sidebar Skip to footer

Updating Sql Server Datetime Columns In Vb.net

When updating records some DateTime values may look like 2015-06-25T01:35:52Z or 13 jun 2014 So when writing query I do: Dim Query As String = 'Update Activities set ActivityDate

Solution 1:

Using Data.SqlClient.SqlCommand.Parameters.Add:

MSDN: SqlCommand.Parameters Property

A parameterized query can handle this datetime conversion for you:

ProtectedSub UpdateCustomerActivityDate(customerID AsInteger, activityDate As DateTime)
    Dim sql AsString = "Update Activities set ActivityDate = @activityDate where customerid = @customerID"Dim cmd AsNew Data.SqlClient.SqlCommand(sql)
    cmd.CommandType = CommandType.Text
    cmd.Parameters.Add("@customerID ", Data.SqlDbType.Int).Value = customerID
    cmd.Parameters.Add("@activityDate ", Data.SqlDbType.DateTime).Value = activityDate
    TryUsing connection AsNew SqlConnection(YourConnectionString)
            connection.Open()
            cmd.Connection = connection
            cmd.ExecuteNonQuery()
        EndUsingCatch ex As Exception
        Throw ex
    EndTryEndSub

Post a Comment for "Updating Sql Server Datetime Columns In Vb.net"