从mysql中日期对应的某列中提取固定值

从mysql中日期对应的某列中提取固定值,mysql,vb.net,Mysql,Vb.net,我在Mysql中有一个表,它有两列date和total number。我想通过编写查询来提取当前日期的总数。这样我就可以使用总数进行进一步计算。提前谢谢 Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MysqlConn = New MySqlConnection MysqlConn.ConnectionSt

我在Mysql中有一个表,它有两列datetotal number。我想通过编写查询来提取当前日期的总数。这样我就可以使用总数进行进一步计算。提前谢谢

 Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString = "Server = localhost; User Id = root; Password=;Database=project"
    Dim Reader As MySqlDataReader
    Try
        MysqlConn.Open()

        Dim todaysdate As String = String.Format("{0:yyyy-MM-dd}", DateTime.Now)
        Dim Query As String
        Query = "select * from project.total_number where date=DateTime.Now"
        Command = New MySqlCommand(Query, MysqlConn)
        Reader = Command.ExecuteReader
        MessageBox.Show("data saved")
        MysqlConn.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        MysqlConn.Dispose()

    End Try

到目前为止,我只做了Datetime。现在显示错误。我想在计算中使用查询结果,您使用的是
Datetime。现在在
WHERE
条件(
WHERE date=Datetime.Now
)中,将其视为字符串文本,这就是您收到错误的原因。把它改成下面的

Query = "select * from project.total_number where `date`=" & DateTime.Now;
(或者)最好使用参数化查询,以避免类似SQL注入的情况

Query = "select * from project.total_number where `date`= @dateval"
Command = New MySqlCommand(Query, MysqlConn)
With Command
        .Parameters.AddWithValue("@dateval", DateTime.Now)
End With