database - 向数据集数据表添加新行

标签 database vb.net datatable dataset

所以我有一个问题。我的项目中有一个 MS Access .mdb 数据库文件作为数据集。我正在尝试向要保存到数据库中的数据集添加一个新行(记录)。请在下面找到我正在使用的代码:

    Function CreateUser(userId As String, pwd As String, pin As String, fnam As String, lnam As String, email As String) As Boolean
    Using ta As New dbUsersDataSetTableAdapters.tUserDetailsTableAdapter
        Using dt As New dbUsersDataSet.tUserDetailsDataTable
            Dim newRow As DataRow = dt.NewRow
            newRow("fUserID") = userId
            newRow("fPassword") = pwd
            newRow("fPin") = pin
            newRow("fFirstName") = fnam
            newRow("fLastName") = lnam
            newRow("fEmailAddress") = email
            dt.Rows.Add(newRow)
            Return True
        End Using
    End Using
End Function

我也尝试过使用表适配器的插入查询:

INSERT INTO `tUserDetails` (`fUserID`, `fPassword`, `fPin`, `fFirstName`, `fLastName`, `fEmailAddress`) VALUES (?, ?, ?, ?, ?, ?)

但我不确定如何编写代码?

有什么帮助吗?

----------------------------------------编辑---- ----------------------------------

好的,这就是我现在正在尝试的:

        Using ds As New dbUsersDataSet
    Using ta As New dbUsersDataSetTableAdapters.tUserDetailsTableAdapter
            ta.InsertQuery(userId, pwd, pin, fnam, lnam, email)
            ta.Update(ds)
            Return True
        End Using
    End Using

我也试过这个:

        Using ta As New dbUsersDataSetTableAdapters.tUserDetailsTableAdapter
            ta.InsertQuery(userId, pwd, pin, fnam, lnam, email)
            Return True
        End Using

这是 TableAdapter 的 INSERT 查询:

INSERT INTO `tUserDetails` (`fUserID`, `fPassword`, `fPin`, `fFirstName`, `fLastName`, `fEmailAddress`) VALUES (?, ?, ?, ?, ?, ?)

然而,它仍然根本不起作用,我很困惑。数据库没有任何变化。

最佳答案

好的,所以我解决了我的问题:

        Using ds As New dbUsersDataSet
        Using ta As New dbUsersDataSetTableAdapters.tUserDetailsTableAdapter
            Using dt As New dbUsersDataSet.tUserDetailsDataTable
                ta.Fill(dt)
                ta.InsertQuery(userId, pwd, pin, fnam, lnam, email)
                ta.Update(dt)
                Return True
            End Using
        End Using
    End Using

我很确定这是因为我忽略了 Fill 方法。

无论如何,为你的帮助干杯 jmcilhinney!

关于database - 向数据集数据表添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27663012/

相关文章:

javascript - DataTable 导致 javascript 停止执行 - 控制台中没有错误

mysql - 一次多次插入然后更新与按事件性能插入值

javascript - 过多的短 PHP 文件

python - Flask 后台网页抓取和数据库更新

.net - '1-dimensional array of byte' 类型的值无法转换为字节

c# - 如何为特殊方法抑制 "formatting is specified but argument is not iformattable"消息

java - 将额外参数传递给 jQuery 数据表的 Java Controller

javascript - 如何使用复选框动态显示/隐藏列

java - 从两个数据库同步获取结果

mysql - 如何将 vb.net 连接到 ONLINE MySQL 数据库?