【问题标题】:VB.NET Missing ID on new datarow in datasetVB.NET 在数据集中的新数据行上缺少 ID
【发布时间】:2021-06-08 18:32:56
【问题描述】:

我在建立新数据集条目的 ID 时遇到问题。新条目为 ID 返回 NullValue,但如果我得到 item(1) 或更大,那么我得到正常数据。

    Dim id as integer

    dbProvider = "Provider=Microsoft.JET.OLEDB.4.0;"
    TheDatabase = "\dbMag.mdb"
    MyDocFolder = Directory.GetCurrentDirectory()
    FullDatabasePath = MyDocFolder & TheDatabase
    dbSource = "Data Source = " & FullDatabasePath
    con.ConnectionString = dbProvider & dbSource

    Try
        con.Open()
        sqlString = "SELECT * FROM mag"
        da = New OleDb.OleDbDataAdapter(sqlString, con)
        da.Fill(ds, "mag")
        con.Close()
    Catch ex As Exception
        MsgBox("Błąd: ", Err.Description)
    End Try

    Dim cb As New OleDb.OleDbCommandBuilder(da)

    dsNewRow = ds.Tables("mag").NewRow()
    With dsNewRow
        'do something
    end with
    ds.Tables("mag").Rows.Add(dsNewRow)
    da.Update(ds, "mag")
    lastrow = ds.Tables("mag").Rows.Count - 1
    id = ds.Tables("mag").Rows(lastrow).Item(0)

id 正在返回 NullValue

【问题讨论】:

  • 由数据库自动生成的 ID 不会神奇地出现在您的应用程序中。你必须把它们拉回来。使用像 SQL Server 这样的适当数据库,这非常简单。使用 Access,它的参与度更高。阅读this,您可能会受益。一旦您使用该信息解决了您的问题,请务必发布您的问题的答案并接受它,如果其他人尚未发布答案。问题不应悬而未决。

标签: vb.net datatable datagrid dataset dataadapter


【解决方案1】:

找到解决方案:

        Dim identity = "SELECT @@IDENTITY"
        Dim cmd = New OleDb.OleDbCommand(identity, con)
        id = cmd.ExecuteScalar()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    相关资源
    最近更新 更多