-1

Im running this query at my SQL and it is good.

INSERT INTO db.tbl (col1, col2, col3) values ('1.68','2014/01/01 05:00:00', Date_add(col2, interval col1 day_second));

My code is not working but the computation at last column is wrong

cmd = New Odbc.OdbcCommand("INSERT INTO room.save(exacttime, depletion_time, time_inputed) values ('" & lblexact.Text & "','" & Trim(TextBox6.Text) & "',' Date_add( exacttime, interval depletion_time day_second)')", con)           
cmd.ExecuteNonQuery()
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143

2 Answers2

1

Always use parameterized/prepared SQL queries. Here's how your code should look like.

Using con As New Odbc.OdbcConnection("...")
    Using cmd As New Odbc.OdbcCommand()

        cmd.Connection = con
        cmd.CommandText = <!--
            INSERT INTO db.tbl (
                col1, 
                col2, 
                col3
            ) VALUES (
                @p1,
                @p2,
                @p3
            );
        -->.Value

        Dim value1 As Double = Double.Parse(labeldatetime.Text)
        Dim value2 As Date = Date.Parse(textboxtime.Text)
        Dim value3 As Date = value2.AddSeconds(value1)

        cmd.Parameters.AddWithValue("@p1", value1)
        cmd.Parameters.AddWithValue("@p2", value2)
        cmd.Parameters.AddWithValue("@p3", value3)

    End Using
End Using

Note that the you don't need a XComment, I just used it to make it more readable.

Community
  • 1
  • 1
Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
0

PS: I am not familiar with vb.net, so not sure if this is the right way to do (So, no downvotes pls!!),. But as per sql, there are some unwanted single quotes that needs to be removed.

cmd = New Odbc.OdbcCommand("INSERT INTO room.save(exacttime, depletion_time, time_inputed) values ('" & lblexact.Text & "','" & Trim(TextBox6.Text) & "',
Date_add( exacttime, interval depletion_time day_second))", con)   
^                                                        ^
|-----------------Single Quotes Removed------------------|  
ngrashia
  • 9,869
  • 5
  • 43
  • 58
  • do i need to use time stamp again?\ – user3795723 Sep 08 '14 at 09:00
  • Youre correct but one last question, could you give 1 more column for every result minus 1 hour. thanks – user3795723 Sep 08 '14 at 09:12
  • Seriously I have no idea of .net. Since I found extra single quotes in sql query, I posted as answer. So, I am unable to understand what is results minus 1 hour.. – ngrashia Sep 08 '14 at 09:32
  • If this helped you solve your current problem, you can accept as answer. Probably you can ask as a separate question which .net experts would be able to help you with. – ngrashia Sep 08 '14 at 09:32
  • sir, can you help me again with my format insert I just add 1 column and 1 data and it is running in my SQL please last.... cmd = New Odbc.OdbcCommand("INSERT INTO room.save( exacttime, depletion_time, expected_dip_time, expected_dip_time2) values ('" & lblexact.Text & "','" & Trim(TextBox6.Text) & "',Date_add( exacttime, interval depletion_time day_second)) " & ",DATE_SUB(Date_add( exacttime, interval depletion_time day_second), interval 1 hour)", con) – user3795723 Sep 09 '14 at 07:22
  • Try this: cmd = New Odbc.OdbcCommand("INSERT INTO room.save( exacttime, depletion_time, expected_dip_time, expected_dip_time2) values ('" & lblexact.Text & "','" & Trim(TextBox6.Text) & "',Date_add( exacttime, interval depletion_time day_second) ,DATE_SUB(Date_add( exacttime, interval depletion_time day_second), interval 1 hour)) ", con) – ngrashia Sep 09 '14 at 07:26