将datagridview记录添加到数据库 [英] adding datagridview records to Database

查看:104
本文介绍了将datagridview记录添加到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,我想知道我的代码有什么问题.我想做的是,当我单击ADD按钮时,将输入的datagridview记录添加到数据库中.但是我遇到了一个错误(包含在备注中),请帮助我,我需要在星期五完成论文,非常感谢.

Greetings, I would like to know what''s wrong with my code. What i want to do is add the inputted datagridview records in the database when I clicked the ADD button. But I''m getting an error (included in the remark), pls help me I need to finish my thesis on Friday, thank you very much.

da2 = New OleDbDataAdapter("SELECT * FROM PurchaseDB2", conDB)
        da2.Fill(ds2, "PurchaseDB2")
        Dim newRow2 As DataRow
        For i = 0 To poDG.Rows.Count - 1
            newRow2 = ds2.Tables(1).NewRow
            col1 = poDG.Rows.Item(i).Cells(0).Value
            col2 = poDG.Rows.Item(i).Cells(1).Value
            col3 = poDG.Rows.Item(i).Cells(2).Value
            col4 = poDG.Rows.Item(i).Cells(3).Value
            col5 = poDG.Rows.Item(i).Cells(4).Value
            newRow2(0) = col1
            newRow2(1) = col2
            newRow2(2) = col3
            newRow2(3) = col4
            newRow2(4) = col5
            ds2.Tables(1).Rows.Add(newRow2) ' This row already belongs to another table.
            Dim cb2 As New OleDbCommandBuilder(da2)
            da2.Update(ds2, "PurchaseDB2")
        Next

推荐答案

非常感谢prdshukla,我现在就知道了.现在可以正常工作了.这是我所做的更改:

Thank you very much prdshukla, i get it now and it''s now working properly. Here''s the changes that i made:

da2 = New OleDbDataAdapter("SELECT * FROM PurchaseDB2", conDB)
        da2.Fill(ds2, "PurchaseDB2")
        ds2.Tables("PurchaseDB2").Clear()   ' i added this
        Dim newRow2 As DataRow
        For i = 0 To poDG.Rows.Count - 1
            newRow2 = ds2.Tables("PurchaseDB2").NewRow
            col1 = poDG.Rows.Item(i).Cells(0).Value
            col2 = poDG.Rows.Item(i).Cells(1).Value
            col3 = poDG.Rows.Item(i).Cells(2).Value
            col4 = poDG.Rows.Item(i).Cells(3).Value
            col5 = poDG.Rows.Item(i).Cells(4).Value
            newRow2(0) = col1
            newRow2(1) = col2
            newRow2(2) = col3
            newRow2(3) = col4
            newRow2(4) = col5
            ds2.Tables("PurchaseDB2").Rows.Add(newRow2)
            Dim cb2 As New OleDbCommandBuilder(da2)
            da2.Update(ds2, "PurchaseDB2")
        Next


这篇关于将datagridview记录添加到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆