Alan Alvarez
Alan Alvarez

Reputation: 678

How to insert time into access database (VB Net)

I try to insert a Date value into MS Access database through VB Net but I get the "can't convert xxxxx to timespan" error. I tried this:

ComandoBD.Parameters.Add("@Hora", OleDbType.DBTime).Value = TimeOfDay.ToString("HH:mm:ss")

ComandoBD.Parameters.Add("@Hora", OleDbType.DBTime).Value = TimeOfDay

ComandoBD.Parameters.Add("@Hora", OleDbType.DBTime).Value = time.TimeOfDay

But nothing, In the Access database the time field is set to "Date/Time" because there is no option for Time only.

This is the entire snippet of that section:

For i As Integer = 0 To Form2.DataGridView2.Rows.Count - 2
            Try
                ConexionBD.Open()
                ComandoBD.Connection = ConexionBD
                ComandoBD.CommandText = "INSERT INTO Ventas (IdProducto, Fecha, Hora) VALUES (@IdProducto, @Fecha, @Hora)"
                ComandoBD.Parameters.Add("@IdProducto", OleDbType.VarChar).Value = Form2.DataGridView2.Rows(i).Cells(0).Value
                ComandoBD.Parameters.Add("@Fecha", OleDbType.DBDate).Value = Now

                ComandoBD.Parameters.Add("@Hora", OleDbType.DBTime).Value = time.TimeOfDay

                ComandoBD.ExecuteNonQuery()
                ComandoBD.Dispose()
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            ConexionBD.Close()
        Next

When I do it (for other fields) with OleDbType.VarChar, OleDbType.Boolean or even OleDbType.Binary I don´t have any problem.

Please help.

Thanks.

Upvotes: 0

Views: 2313

Answers (1)

NineBerry
NineBerry

Reputation: 28539

Use

  ComandoBD.Parameters.Add("@Hora", OleDbType.DBTime).Value = DateTime.Now.TimeOfDay

Upvotes: 1

Related Questions