0

我是一个 PHP-MySql 专家,并且是vb.net 和 SQL Server CE 的新手。
如何处理基于日期的记录?

考虑任何表(tbl_demo),其中某些列具有 1 列来存储今天的日期。

 For example (table structure)-     Id     Name       today
                                    --     -----      -----
                                    int    nvarchar   datetime

如果我在表格中有以下数据 -

 Id     Name       today
 --     -----      -----
 1      vikram     11/08/2013 11:16:57 PM

那么我如何通过仅向查询提供11/08/2013来检索此记录?

PS-
我在执行查询时遇到了以下问题(这里是代码片段)-

 Dim Ddate As Date
 Ddate = MonthCalendar1.SelectionRange.Start.ToString

SQLquery = "SELECT id,today FROM tbl_demo WHERE today = '" & Ddate & "'"

错误 :The data type is not valid for the boolean operation.[Data type (if known) = datetime,Data type (if known) = nvarchar]

4

3 回答 3

0

这是一个使用数据阅读器的完整示例。如果您要一次处理一行,这是合适的。如果您需要将所有行保存在内存中或执行进一步的操作,您可能需要使用 ExecuteDataSet 来获取值。

此示例将行值打印到命令行。

Dim recordDate as DateTime = new DateTime(2013,8,11)
Using connection As SqlCeConnection = New SqlCeConnection("Data Source=Database.sdf;Persist Security Info=False;")

    connection.Open()

    Using command As SqlCeCommand = connection.CreateCommand()
        command.CommandText = "Select id, name, today from demo where today = @date"
        Dim dateParameter As SqlCeParameter
        dateParameter = New SqlCeParameter("@date", DbType.DateTime)
        dateParameter.Value = recordDate
        command.Parameters.Add(dateParameter)

        Using reader As SqlCeDataReader = command.ExecuteReader()
            Do While reader.Read()
                Dim id As Integer = reader.GetInt32(0)
                Dim name As String = reader.GetString(1)
                Dim today As DateTime = reader.GetDateTime(2)
                Console.WriteLine("{0,-4} {1,-10} {2,-10}", id, name, today)
            Loop

        End Using

    End Using
End Using

需要注意的一些重要事项:

  • Using 语句会在您完成后自动清除未使用的资源
  • 使用的 sql 语句是参数化的,它允许您传入强类型值而不是字符串。

Console.WriteLine 的第一个参数是字符串格式。{0, -4} 是一个占位符,将由下一个参数填充。-4 表示该值应在 4 个字符宽的列中左对齐显示。有关详细信息,请参阅MSDN 上的String.Format方法文档。

于 2013-08-11T19:32:55.883 回答
0

在试验时......我得到这个工作如下(如果我错了请纠正我)

    Dim Ddate As Date
    Dim d, m, y
    Ddate = MonthCalendar1.SelectionRange.Start.ToString
    d = Ddate.ToString("dd")
    m = Ddate.ToString("MM")
    y = Ddate.ToString("yyyy")  

查询是:

SQLquery = "SELECT billId,c_id,amount FROM tbl_outward WHERE (DATEPART(dd, ddate) = '" & d & "' AND DATEPART(mm, ddate) = '" & m & "' AND DATEPART(yyyy, ddate) = '" & y & "')"
于 2013-08-11T19:06:49.993 回答
0

Though it is not best to have functions in the fields in the where clause (performance wise) this will do the job.

"SELECT id,today FROM tbl_demo WHERE CONVERT(DATETIME,CONVERT(VARCHAR,today,120)) = CONVERT(DATETIME,CONVERT(VARCHAR,'" & Ddate.ToString() & "',120))"

An alternative would be providing the Date directly to ISO format:

"SELECT id,today FROM tbl_demo WHERE CONVERT(DATETIME,CONVERT(VARCHAR,today,120)) = CONVERT(DATETIME,CONVERT(VARCHAR,'" & Ddate.ToString("yyyyMMdd") & "',120))"

I am not sure if it is yyyyMMdd or yyMMdd for SQL CE, so better try both.

According to this MSDN article it should be "yyMMdd" but it could be a typo...

于 2013-08-11T18:36:50.647 回答