首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在vb.net中一步一步地保存(插入) datatable中的数据

如何在vb.net中一步一步地保存(插入) datatable中的数据
EN

Stack Overflow用户
提问于 2020-08-19 14:41:52
回答 3查看 906关注 0票数 0

我正在创建用于将数据从vb.net中的datagridview插入到数据库表Access 2007的代码。但是这个程序是为了插入来自用户的数据,当他在第一行的datagridview中输入数据,然后在下一次(在break by break之后)他在第二,第三下一行中再次输入数据,并在我的项目中单击保存按钮在数据库中插入数据。但问题出现了,当他点击保存第二次插入数据到数据库的过程代码插入所有行旧和新的再次。插入不正确。它重复数据。所以请建议我使用dt for datatable和sql for sql语句的解决方案。

代码语言:javascript
复制
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim sql = "SELECT * FROM RABill "
        maxrow = get_maxrow(sql)

        For i = 0 To DgRAbill.Rows.Count - 2

            Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM RABill  ")
            Dim strInsert As String = "INSERT INTO RABill (Billnumber,RDate,RAmount,Below,Royalti,SD,IT,GSTminus,RAccountname,GstTwelvepercent,Insurance,Other,TotalBill,Remark) VALUES ('" _
                                                                   & CStr(DgRAbill.Rows(i).Cells(1).FormattedValue) & "','" _
                                                                   & CStr(DgRAbill.Rows(i).Cells(2).FormattedValue) & "','" _
                                                                   & CStr(DgRAbill.Rows(i).Cells(3).FormattedValue) & "','" _
                                                                   & CStr(DgRAbill.Rows(i).Cells(4).FormattedValue) & "','" _
                                                                   & CStr(DgRAbill.Rows(i).Cells(5).FormattedValue) & "','" _
                                                                   & CStr(DgRAbill.Rows(i).Cells(6).FormattedValue) & "','" _
                                                                   & CStr(DgRAbill.Rows(i).Cells(7).FormattedValue) & "','" _
                                                                   & CStr(DgRAbill.Rows(i).Cells(8).FormattedValue) & "','" _
                                                                   & ComboBox1.Text & "','" _
                                                                   & CStr(DgRAbill.Rows(i).Cells(10).FormattedValue) & "','" _
                                                                   & CStr(DgRAbill.Rows(i).Cells(11).FormattedValue) & "','" _
                                                                   & CStr(DgRAbill.Rows(i).Cells(12).FormattedValue) & "','" _
                                                                   & CStr(DgRAbill.Rows(i).Cells(13).FormattedValue) & "','" _
                                                                   & CStr(DgRAbill.Rows(i).Cells(14).FormattedValue) & "')"
            cmd = New OleDbCommand(strInsert, conn)
            cmd.ExecuteNonQuery()
            MsgBox("Record  Saved Successfully")
        Next

    End Sub

在上面的代码中,我的datagridview中有额外的行,所以它的代码是rows-2

EN

回答 3

Stack Overflow用户

发布于 2020-08-19 15:49:40

您似乎没有在insert之后清除变量'strInsert‘,所以这可能是您面临的问题?

我很想在MsgBox之后清除这个变量。

非常感谢POC

票数 0
EN

Stack Overflow用户

发布于 2020-08-19 23:03:36

这里有几个关于你的代码片段的问题。

  1. 你的数据库应该可以有重复的条目吗?您可能需要查看主键是如何设置的。

  1. 在这里你确实会遇到一些错误,特别是在数据库连接方面。请查看以下内容以供参考:https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement

  1. 对于实际的复式录入-在保存所有记录后,您不会清空您的datagridview。如果我没理解错的话,用户在DGV中填入一堆行,然后点击保存,然后循环并插入所有行。然后他继续输入并再次点击保存,系统将插入所有这些文件(一些副本)。在保存之后,你应该在你的DGV上做一个.Clear -或者你需要创建一些在保存之间持续存在的计数器,这就是你将在下面的保存按钮中开始你的for循环的地方,它在每次保存后清除它:

单击Dim sql = "SELECT * FROM RABill“maxrow = get_maxrow(sql) For i=0 To DgRAbill.Rows.Count -2\f2 Try Dim strInsert As Button5_Click= "INSERT INTO RABill (Billnumber,RDate,RAmount,Below,Royalti,SD,IT,GSTminus,RAccountname,GstTwelvepercent,保险,其他,TotalBill,备注)值('“_& CStr(DgRAbill.Rows(i).Cells(1).FormattedValue) & "','”_& CStr(DgRAbill.Rows(i).Cells(2).FormattedValue) & "','“_& CStr(DgRAbill.Rows(i).Cells(3).FormattedValue) & "','”_& CStr(DgRAbill.Rows(i).Cells(4).FormattedValue) & "','“_& CStr(DgRAbill.Rows(i).Cells(5).FormattedValue) & "','”_& CStr(DgRAbill.Rows(i).Cells(6).FormattedValue) & "','“_& CStr(DgRAbill.Rows(i).Cells(7).FormattedValue) & "','”_& CStr(DgRAbill.Rows(i).Cells(8).FormattedValue) & "','“_& ComboBox1.Text & "','”_& CStr(DgRAbill.Rows(i).Cells(10).FormattedValue) & "','“_& CStr(DgRAbill.Rows(i).Cells(11).FormattedValue) & "','”_& CStr(DgRAbill.Rows(i).Cells(12).FormattedValue) & "','“_& CStr(DgRAbill.Rows(i).Cells(13).FormattedValue) & "','”_& CStr(DgRAbill.Rows(i).Cells(14).FormattedValue) & "')“cmd =新建OleDbCommand(strInsert,conn) cmd.ExecuteNonQuery() MsgBox(“成功保存记录”) DgRabill.Rows.Clear() Catch ex As Exception MessageBox.Show(“在行插入记录时出错”&i& vbCrLf & ex.Message) End Try Next End Sub

票数 0
EN

Stack Overflow用户

发布于 2020-08-25 17:03:00

您可以使用以下代码将DataGridView数据绑定并保存到数据库。

代码语言:javascript
复制
Dim connString As String = ""
Private ds As DataSet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ds = New DataSet()

    Using con As SqlConnection = New SqlConnection(connString)
        con.Open()
        Dim adp As SqlDataAdapter = New SqlDataAdapter("select * from RABill", con)
        adp.Fill(ds)
        Dim dt As DataTable = ds.Tables(0)
        DataGridView1.DataSource = dt
    End Using
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Using con As SqlConnection = New SqlConnection(connString)
        con.Open()
        Dim adp As SqlDataAdapter = New SqlDataAdapter("select * from RABill", con)
        Dim build As SqlCommandBuilder = New SqlCommandBuilder(adp)
        adp.Update(ds.Tables(0))
    End Using
End Sub

无论您在DataGridView中编辑什么,请单击button1,这些记录将保存在数据库中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63481352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档