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

时间:2015-09-11 21:11:07

标签: mysql vb.net

我在Mysql中有一个表,其中有两列日期总数。我想通过编写query来提取当前日期的总数。我可以使用总数进行进一步计算。提前谢谢

 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.Now显示错误。我想在计算中使用查询结果

1 个答案:

答案 0 :(得分:0)

您在DateTime.Now条件(WHERE)中使用where date=DateTime.Now,就像它是字符串文字一样,这就是您收到错误的原因。将其更改为

Query = "select * from project.total_number where `date`=" & DateTime.Now;

(OR)更好地使用参数化查询来避免SQL注入,如

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