【问题标题】:My function returns an empty DataTable (VB.NET)我的函数返回一个空的 DataTable (VB.NET)
【发布时间】:2016-04-11 17:43:39
【问题描述】:

我正在使用带有 Microsoft Access 的 VB.NET。

我创建的函数有问题。我只希望函数返回一个充满信息的 DataTable 来制作我的报告,并使用我给出的参数。我正在使用Between子句...我首先认为子句“Between”不适用于Access(如“Limit”),但我在Access中创建了一个查询,并且效果很好!

看到这个,我得出的结论是,由于某种原因,我的 OleDbDataAdapter 没有按照应有的方式填充我的 DataTable ...我不明白为什么,因为我遵循与其他函数相同的模式(使用不同的查询,显然)。

函数的代码是这样的:

Protected Friend Function reporte(ByVal tipo As String, ByVal fechInicio As String, ByVal fechFinal As String) As DataTable
    Dim cmd As String = ""
    Dim p As New DataTable
    If (tipo.Equals("general")) Then
        cmd = "Select Cod_Producto,Serial,Lotpallet,Fecha_Ingreso,Producto,Modelo,Descripcion,Precio,Cantidad From Productos WHERE Fecha_Ingreso BETWEEN '[" & fechInicio & "]' AND '[" & fechFinal & "]'"
    ElseIf (tipo.Equals("por producto")) Then
        cmd = "Select Cod_Producto,Serial,Lotpallet,Fecha_Ingreso,Producto,Modelo,Descripcion,Precio,Cantidad From Productos WHERE Producto='[" & PantallaPrincipal.REP.ComboModel.SelectedItem & "]' AND Fecha_Ingreso BETWEEN '[" & fechInicio & "]' AND '[" & fechFinal & "]'"
    End If
    Try
        con.Open()
        adapter = New OleDbDataAdapter(cmd, con)
        adapter.Fill(p)
        adapter.Dispose()
        comando.Dispose()
        con.Close()
    Catch ex As Exception
        con.Close()
        MsgBox("Problemas en la consulta: " + ex.Message(), MsgBoxStyle.Critical)
    End Try
    Return p
End Function

可能是什么问题?

【问题讨论】:

  • 我已经用过了,但没用。另外,我的表单没有用于执行 SQL 注入的文本框...
  • 我按照你说的做了测试,是的,我的变量在我编程时成功存储了数据
  • 日期正在存储,我的查询也是
  • 看!适配器未能填充 DataTable! i.imgur.com/cawSQnK.png

标签: vb.net ms-access datatable


【解决方案1】:

嗯......显然,参数化查询是解决方案......也许我写错了......我想我混淆了......对不起:

Protected Friend Function reporte(ByVal tipo As String, ByVal fechInicio As String, ByVal fechFinal As String) As DataTable
    Dim cmd As String = ""
    Dim table As New DataTable
    If (tipo.Equals("general")) Then
        cmd = "Select Cod_Producto,Serial,Lotpallet,Fecha_Ingreso,Producto,Modelo,Descripcion,Precio,Cantidad From Productos WHERE Fecha_Ingreso BETWEEN @fein AND @feed"
    ElseIf (tipo.Equals("por producto")) Then
        cmd = "Select Cod_Producto,Serial,Lotpallet,Fecha_Ingreso,Producto,Modelo,Descripcion,Precio,Cantidad From Productos WHERE Producto='[" & PantallaPrincipal.REP.ComboModel.SelectedItem & "]' AND Fecha_Ingreso BETWEEN @fein AND @feed"
    End If
    Try
        con.Open()
        comando = New OleDbCommand(cmd, con)
        comando.Parameters.AddWithValue("@fein", fechInicio)
        comando.Parameters.AddWithValue("@feed", fechFinal)
        adapter = New OleDbDataAdapter(comando)
        adapter.Fill(table)
        comando.Dispose()
        adapter.Dispose()
        con.Close()
    Catch ex As Exception
        con.Close()
        MsgBox("Problemas en la consulta: " + ex.Message(), MsgBoxStyle.Critical)
    End Try
    Return table
End Function

感谢您的帮助,再次抱歉 Bjørn-Roger Kringsjå。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2011-02-13
    • 2021-08-17
    • 1970-01-01
    相关资源
    最近更新 更多