【问题标题】:Using Structures in VB.Net with Datatables在 VB.Net 中将结构与数据表一起使用
【发布时间】:2017-07-13 02:33:56
【问题描述】:

我最近在 vb.net 中使用结构。所以我试图利用它们来存储信息表,但我收到一个错误,说该表为空。我认为这是因为它没有声明为新实例,但结构不接受“新”。

有什么想法吗?

这是我用的;

它在这一行崩溃:ds.Tables.Add(dt.AutoMatic)

Public Structure Reminder
    Public AutoMatic As DataTable
    Public Manual As DataTable
End Structure
Public Function ReturnReminders() As Reminder
    Dim DateRangeA As Date = Date.Today
    Dim DateRangeB As Date = Date.Today
    DateRangeA = DateRangeA.AddMonths(-1)
    DateRangeA = DateRangeA.ToString("MM/dd/yyyy") : DateRangeB = DateRangeB.ToString("MM/dd/yyyy")
    Dim Sql As String = "SELECT reminder_id, Subject_Line, Body, LifeTime FROM ucert_reminder_tbl WHERE Date_Remind BETWEEN #" & DateRangeA & "# AND #" & DateRangeB & "# AND Hide = 0 AND AutoRem = 1" ' reminder_id = " & Id
    Dim Items As New ArrayList
    Dim ds As New DataSet
    Dim dt As New Reminder

    ConOpen()
    ds.Tables.Add(dt.AutoMatic)
    Dim da As New OleDbDataAdapter(Sql, _myConnection)
    da.Fill(dt.AutoMatic)
    ConClose()
    Return dt

【问题讨论】:

  • 结构不能为空,它们总是有一个默认值,所有字段都初始化为某个东西(类类型的字段除外,这是唯一可以为空的inside我>它)。这是导致错误的其他原因。错误是在哪一行抛出的?
  • 数据表什么都没有。尽管该结构的意义令人费解——集合或 DataSet 会更合适。此外,永远不要使用 SQL 参数为 SQL 连接字符串
  • ds.Tables.Add(dt.AutoMatic) 在 ConOpen() 之后
  • 除了@Plutonix 提到的所有内容之外,DataAdapter 可以自己打开和关闭连接,因此您可以删除那些 ConOpen 和 ConClose 行。
  • ....但是当您只需要 cmd.ExecuteReader 时,仅本地的 DataAdapter 有点浪费

标签: vb.net datatables structure


【解决方案1】:

视觉文森特是对的, 数据表为空。

添加了这一行

dt.AutoMatic = New DataTable

现在可以正常工作了。

感谢大家的帮助! Defo 研究 Sql 参数以备将来使用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    相关资源
    最近更新 更多