2011-05-24 77 views
1

它使用1个参数(i_index),但如果我也使用i_datum,则会出现如下错误:“从字符串”park_id = 100“转换为键入”Long“无效。是否可以在dataview.rowfilter中使用2个参数?怎么样?

Public Function detail_kalender(ByVal i_index As Int16, ByVal i_datum As Date) As DataRowView 
    Dim dv As DataView 
    Dim anyrow As DataRowView 
    dv = New DataView 

    With dv 

     .Table = myds.Tables("kalender") 
     .AllowDelete = True 
     .AllowEdit = True 
     .AllowNew = True 
     .RowFilter = "park_id = " & i_index And "datum =" & i_datum 


    End With 
    anyrow = dv.Item(0) 'geeft de eerste rij van de dataview dv 

    ' Simple bind to a TextBox control 
    dv = mydt_parken.DefaultView 
    Return anyrow 
    dv.Dispose() 
    dv = Nothing 

End Function 
+0

你是什么'i_'前缀的意义是什么? – 2011-05-24 14:47:21

+0

如果类型是整数,我通常使用i! – Rachid 2011-05-25 09:23:30

+0

那你为什么在i_datum上使用它? – Smudge202 2011-05-25 10:02:55

回答

0

看着你有没有为行过滤器的代码,这将转化为:

.RowFilter = "park_id = 100datum = something" 

通知缺乏的park_id与一场之间的空间。您还需要添加“和”(我想?)

尝试:

.RowFilter = String.Format("park_id = {0} and datum = {1}", i_index.Tostring, i_datum.ToString) 

您可能需要鼓捣这包括根据类型数据的撇号(即改变

... and datum = **'**{1}**'** 

如果它是一个字符串)

编辑:在回应您的评论。

看看this page的一些有用的提示。对于使用#符号的日期。

.RowFilter = String.Format("park_id = {0} and datum = #{1}#", i_index.Tostring, i_datum.ToString) 

编辑:在回答你的第二个评论(出现FormatException“字符串未被识别为有效的DateTime。”):

这是一个有点棘手。我会列出一个可能的快速解决方案,但决不是最好的解决方案。

Dim customDateFormat As String = "MM/dd/yyyy hh:mm:ss" 
.RowFilter = String.Format("park_id = {0} and datum >= #{1}# and datum <= #{2}#", 
          i_index.ToString, 
          New DateTime(i_datum.Year, i_datum.Month, i_datum.Day, 0, 0, 0).ToString(customDateFormat), 
          New DateTime(i_datum.Year, i_datum.Month, i_datum.Day, 23, 59, 59).ToString(customDateFormat)) 

基本上,有机会,当你比较你的日期对日期时间在数据库中,你要忽略的时间? (在这里做一个假设)。这样做的一种方法是将您的数据库数据与日期进行比较,确保它在00:00 AM到23:59:59 PM之间。

我已经包含一个customDateFormat字符串,如果需要可以篡改,以反映您的语言环境。我知道日期常量忽略语言环境,但我不知道它在RowFilter中作为一个字符串做了什么,以及数据库语言环境是否对它有任何影响。如果上述不起作用,您可以更改日期格式字符串以匹配您的语言环境,以查看是否有帮助。

+0

park_id是一个整数,而i_datum是日期类型!那么,那么语法是什么? – Rachid 2011-05-24 14:06:53

+0

如果我使用这些解决方案,我得到一个formatException“字符串未被识别为有效的DateTime。”因为我的数据库中的类型是dateTime!你有这个解决方案吗? – Rachid 2011-05-25 09:34:39

0

尝试使用,而不是:

.RowFilter = "park_id = " & i_index & " And datum =" & i_datum 
相关问题