簡體   English   中英

VB.NET 數據集中的新數據行缺少 ID

[英]VB.NET Missing ID on new datarow in dataset

我在建立新數據集條目的 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

找到解決方案:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM