简体   繁体   English

vb.net INSERT CURDATE()到MySQL问题

[英]vb.net INSERT CURDATE() to MySQL issue

In a windows application I have a query that im trying to insert the current date on my mysql database 在Windows应用程序中,我有一个查询,我试图在我的mysql数据库中插入当前日期

query below: 查询如下:

SqlVentaCasas1 = "INSERT INTO VentaCasasHistory (ID, Direccion, Estatus, Precio, " & _
                                "NumeroDias, FechaHoy, Agente, Compania, Unidad, Ciudad ) VALUES ('" & _
                                AddIDCasas2 & "','" & AddDireccionCasas2 & "','" & AddEstatusCasas2 & "'," & AddPrecioCasas2 & ", 0, CURDATE(), '" & AgenteNameCasas2 & "','" & AgenteCompaniaCasas2 & "', '" & AddUnidadCasas2 & "', '" & AddCiudadCasas2 & "' );"


                        CommandVentaCasas1 = New MySqlCommand(SqlVentaCasas1, con)
                        CommandVentaCasas1.CommandType = CommandType.Text
                        CommandVentaCasas1.ExecuteNonQuery()

when i input the values that i want to insert into mysql everything runs smoothly except that when i check the database for the date entered i find myself with the 0000-00-00 当我输入我想要插入到mysql中的值时,一切都运行顺利,但是当我检查数据库中输入的日期时,我发现自己与...

on the other hand, when i run the same query from PHPmyAdmin 另一方面,当我从PHPmyAdmin运行相同的查询时

INSERT INTO `VentaCasasHistory`(`ID`, `Direccion`, `Estatus`, `Precio`, `NumeroDias`, `FechaHoy`, `Agente`, `Compania`, `Unidad`, `Ciudad`) VALUES ([String1],[String2],[String3],[Integer1],[Integer2],CURDATE(),[String4],[String5],[String6],[String7])

where FechaHoy is CURDATE() is does insert correctly the date that should be. 其中FechaHoy是CURDATE()确实插入了应该的日期。

I cant seem to find what is the problem, As i understand it will only insert 0000-00-00 if there is something wrong with the date when been entered. 我似乎无法找到问题所在,据我了解,如果输入日期有问题,它只会插入稿件。

the reason i kept both queries "identical" was to understand what im doing wrong and I know that they can be subject to SQL injection. 我保持两个查询“相同”的原因是为了理解我做错了什么,我知道他们可以接受SQL注入。

your help would be very appreciated. 非常感谢你的帮助。

Leo P. Leo P.

this is the code i have for the last piece that you told me. 这是我告诉我的最后一篇文章的代码。

Dim StringDate As String

    StringDate = Date.Today.ToString("yyyy-MM-dd")

SqlVentaCasas1 = "INSERT INTO VentaCasasHistory (ID, Direccion, Estatus, Precio, " & _
                                "NumeroDias, FechaHoy, Agente, Compania, Unidad, Ciudad ) VALUES ('" & _
                                AddIDCasas2 & "','" & AddDireccionCasas2 & "','" & AddEstatusCasas2 & "'," & AddPrecioCasas2 & ", 0, STR_TO_DATE('" & StringDate & "', GET_FORMAT(DATE,'ISO')), '" & AgenteNameCasas2 & "','" & AgenteCompaniaCasas2 & "', '" & AddUnidadCasas2 & "', '" & AddCiudadCasas2 & "' );"

unfortunately it does not work and i still get the 0000-00-00. 不幸的是它不起作用,我仍然得到了这个程序。

CURDATE() should really be specified as default value used in CREATE TABLE . 应该将CURDATE()确定为CREATE TABLE使用的默认值。 In other words, you don't need to put it in as part of a query as when you insert a new record the field with default value set to CURDATE() will have the date automatically inserted. 换句话说,您不需要将其作为查询的一部分放入,因为当您插入新记录时,默认值设置为CURDATE()的字段将自动插入日期。

F.ex: F.ex:

CREATE TABLE MyTable
(
    ID int NOT NULL,
    MyName varchar(30) NOT NULL,
    MyDate datetime NOT NULL DEFAULT CURDATE(),
    PRIMARY KEY (ID)
)

INSERT INTO MyTable (MyName) VALUES ('Epistemex')

Will set the MyDate field to CURDATE() . MyDate字段设置为CURDATE()

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM