我在我的SQL上运行此查询,这很好。
INSERT INTO db.tbl (col1, col2, col3) values ('1.68','2014/01/01 05:00:00', Date_add(col2, interval col1 day_second));
我的代码无效但最后一列的计算错误
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()
答案 0 :(得分:1)
始终使用parameterized/prepared SQL queries。以下是您的代码的外观。
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
请注意,您不需要XComment
,我只是用它来使其更具可读性。
答案 1 :(得分:0)
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------------------|