如何在VB.net中将多行从datagridview插入到SQL数据库?

pan*_*bar 3 vb.net sql-server datagridview

我制作了一个名为 的表格form1.vb。
我有 4 个名为supid, supname, supmobile,的文本框suploc。
我有一个名为 的 DataGridView datagridview1。
我正在 DataGridView 中传输这 4 个文本框数据,但是当我尝试在 SQL 数据库中发送 DataGridView 行时,它仅发送 1 行。

当我单击插入按钮时,我希望在 SQL Server 中立即插入多行。
关于C语言的教程有很多,但是没有关于VB.Net.
请帮助如何在VB.Net.

我的代码:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    If TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Then
        MessageBox.Show("PLEASE FILL ALL FIELDS")
    Else
        connection.Open()
        Dim thequery As String = "select*from supplierdata where supmobile = @supmobile"
        Dim cmd1 As SqlCommand = New SqlCommand(thequery, connection)
        cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)

        Dim reader As SqlDataReader = cmd1.ExecuteReader()

        If reader.HasRows Then
            MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
        Else
            reader.Close()
            command = New SqlCommand("INSERT INTO supplierdata VALUES (@supid,@supname,@supmobile,@suploc)", connection)
            Dim i As Integer = 0
            For Each row As DataGridViewRow In DataGridView1.Rows
                command.Parameters.Add("supid", SqlDbType.Int).Value = DataGridView1.Rows(i).Cells(0).value
                command.Parameters.Add("supname", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(1).Value
                command.Parameters.Add("supmobile", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(2).Value
                command.Parameters.Add("suploc", SqlDbType.NChar).Value = DataGridView1.Rows(i).Cells(3).Value

                i = command.ExecuteNonQuery()
            Next

            MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
            connection.Close()
            command.Dispose()
        End If
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

Mar*_*ary 5

  1. 在你的第一个 If 语句中使用OrElse而不是Or. 一旦检测到 True,就会停止评估。
  2. 保持本地连接。它们应在最后一刻打开,并尽快关闭和处置。一个Using...End Using块会处理这个问题。
  3. 如果您需要的只是计数,那么只要求计数。不要检索不使用的数据。没有DataReader必要。只需使用ExecuteScalar.
  4. 使用参数做得很好!
  5. 在 Sql 查询中使用的参数名称必须与添加参数时的名称相匹配。(对于 Access 来说并非如此,因为参数添加的顺序是唯一重要的事情。)
  6. 现在到真正的问题然后您在 For Each 循环中Dim i As Integer = 0使用。DataGridView1.Rows(i).Cells(0).ValueRow 始终是第一行,并且它在循环中永远不会改变。这就是为什么“它只发送 1 行”
  7. 您不想继续在For Each循环中添加参数。
  8. 另一件事 - 如果subid是自动增量列,则不要发送该列的任何数据。

未经测试的代码。我没有你的表格或数据库。

Private Sub OPCode2()
    If TextBox2.Text = "" OrElse TextBox3.Text = "" OrElse TextBox4.Text = "" Then
        MessageBox.Show("PLEASE FILL ALL FIELDS")
        Return
    End If
    Dim count As Integer
    Try
        Using cn As New SqlConnection("Your connection string")
            Dim thequery As String = "select Count(*) from supplierdata where supmobile = @supmobile;"
            Using cmd1 As SqlCommand = New SqlCommand(thequery, cn)
                cmd1.Parameters.AddWithValue("@supmobile", TextBox3.Text)
                cn.Open()
                count = CInt(cmd1.ExecuteScalar())
            End Using
        End Using
    Catch ex As Exception 'You probably want to catch more specific exceptions
        'and handle them separately
        MessageBox.Show(ex.Message)
    End Try
    If count > 0 Then
        MessageBox.Show("MOBILE NO IS ALREADY REGISTERED")
        Return
    End If
    Try
        Using cn As New SqlConnection("Your connection string")
            Using cmd2 As New SqlCommand("INSERT INTO supplierdata (supid,supname,supmobile,suploc)VALUES (@supid,@supname,@supmobile,@suploc)", cn)
                cmd2.Parameters.Add("@supid", SqlDbType.Int)
                cmd2.Parameters.Add("@supname", SqlDbType.NChar)
                cmd2.Parameters.Add("@supmobile", SqlDbType.NChar)
                cmd2.Parameters.Add("@suploc", SqlDbType.NChar)
                cn.Open()
                For Each row As DataGridViewRow In DataGridView1.Rows
                    With cmd2
                        .Parameters("@supid").Value = row.Cells(0).Value
                        .Parameters("@supname").Value = row.Cells(1).Value
                        .Parameters("@supmobile").Value = row.Cells(2).Value
                        .Parameters("@suploc").Value = row.Cells(3).Value
                    End With
                    cmd2.ExecuteNonQuery()
                Next
            End Using 'You probably want to catch more specific exceptions
            'and handle them separately
        End Using
    Catch ex As Exception 'You probably want to catch more specific exceptions
        'and handle them separately
        MessageBox.Show(ex.Message)
    End Try
    MessageBox.Show("CUSTOMER REGISTERED SUCCESFULLY")
End Sub
Run Code Online (Sandbox Code Playgroud)

编辑 如果supid是自动增量列,请将其从参数中删除。将插入查询更改为 "INSERT INTO supplierdata (supname,supmobile,suploc)VALUES (@supname,@supmobile,@suploc)" 然后您需要删除行 cmd2.Parameters.Add("@supid", SqlDbType.Int) 并且 .Parameters("@supid").Value = row.Cells(0).Value 仅当supid是并且自动增量(标识列)时才执行此操作