Visual Basic 更新列正在查询中进行算术运算

Visual Basic update column is doing arthmetic in query

我有一个页面显示 SQL 数据库中的结果。然后我有另一个页面让我编辑我想编辑的任何行。其中一个字段是日期。如果通过我的其中一个页面将其添加到数据库中,它将以 (YEAR-MN-DY)(2014-04-11) 格式输入。当我去更新日期时,它会对日期进行算术运算。例如。如果当前日期是 2014-04-11 而我 update/change 到 2010-01-01 的日期,它将用“2008”替换日期,即 2010 -1 - 1.

变量是通过HTML形式接收的字符串。

strSQL = "UPDATE sales SET cust_id = " & intcust_id & ", agent_id = " & intagent_id & ", saledate = " & strsaledate & " WHERE sale_id = " & intsale_id & ""

是SQL查询我是运行.

此外,数据库中的 DATE 是 VARCHAR2,在我的 VB 代码中用作字符串。我保持这种方式是因为并非每个人都输入相同的日期,这是为了简单起见。

发生减法是因为日期没有被解释为日期,而是被解释为数字,因为它缺少引号。

之前

strSQL = "UPDATE sales SET cust_id = " & intcust_id & ", agent_id = " & intagent_id & ", saledate = " & strsaledate & " WHERE sale_id = " & intsale_id & ""

之后

strSQL = "UPDATE sales SET cust_id = " & intcust_id & ", agent_id = " & intagent_id & ", saledate ='" & strsaledate & "' WHERE sale_id = " & intsale_id & ""

WorkSmarter 的回答解决了这个问题。但是我认为你不应该使用字符串连接。它对 sql-injections 是完全开放的,并且使用参数确实更好,更简单并且更不容易出错。像

strSQL = "UPDATE sales SET cust_id = @custid, agent_id = @agentid, saledate = @salesdate WHERE sale_id = @saleid"
set custid = cmd.CreateParameter("@custid", adChar,adInput,10, incust_id)
set agentid = cmd.CreateParameter("@cagentid", adInteger,adInput,0, ) ...

我假设您有一个名为 cmd 的 ADODB.Command。通过这样做,您可以使您的代码更安全,并且在我看来,更具可读性,因为您不必一直担心引号和单引号。 sql命令和涉及的params/values有明显区别。 您可以在 http://www.w3schools.com/asp/met_comm_createparameter.asp

上找到有关参数的良好文档