Search code examples
vb.netdatetimesql-server-ce

Inserting DateTime on Vb.Net SQL Server CE


I want to insert into a SQL Server CE (.sdf) a birth date, read from a .csv file.

The TextLine(5) is the date in string format: ddMMyyyy

I convert it to a VB DateTime(), it converts at this way: Day/Month/Year

After that, I insert it into SQL Server CE, everything good but the problem is, on the database is always showing as 01/01/1900, what am I doing wrong?

Also I put a Console.WriteLine to check if there is any error, no errors as I expected.

apadronado.fecnac is an DateTime also my provider is an cultureinfo is an en-US

I have the following code:

apadronado.fecnac = DateTime.ParseExact(TextLine(5).ToString & " 00:00", _
  "yyyyMMdd HH:mm", provider)
cmd.CommandText = "INSERT INTO personas VALUES ('" & apadronado.ci & "','" & _
   apadronado.nombre & "','" & apadronado.apellido & "','" & _
   apadronado.dpto & "','" & apadronado.ciudad & "'," & _
   apadronado.fecnac & ")"
Console.WriteLine(cmd.CommandText)
Try
  cmd.ExecuteNonQuery()
Catch ex As Exception
  Console.WriteLine(ex)
End Try

What am I doing wrong?

Thanks


Solution

  • You need to add single quotes around the date value.

    Change:

    apadronado.ciudad & "'," & _
       apadronado.fecnac & ")"
    

    to:

    apadronado.ciudad & "','" & _
       apadronado.fecnac & "')"