我做了一个名为form1.vb的表格。
我有4个名为supidsupnamesupmobilesuploc的文本框。
我有一个名为datagridview1的DataGridView。
我正在DataGridView中传输这4个TextBoxes数据,但是当我尝试在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

最佳答案

第一个If语句中的

  • 使用OrElse而不是Or。一旦检测到True,这将停止评估。
  • 保持本地连接。它们应在最后一刻打开,并在可能的情况下尽快关闭和处置。 Using...End Using块将解决这一问题。
  • 如果您需要的只是计数,则只要求计数。不要检索不使用的数据。无需DataReader。只需使用ExecuteScalar即可。
  • 使用参数很好!
  • 添加参数时,在Sql查询中使用的参数名称必须与您的名称匹配。 (对于Access而言,不是唯一的参数添加顺序很重要的访问。)
  • 现在是真正的问题Dim i As Integer = 0然后在For Each循环中使用DataGridView1.Rows(i).Cells(0).Value。行始终是第一行,它在循环中永远不会改变。这就是为什么“仅发送1行”
  • 您不想继续在For Each循环中添加参数。
  • 还有一件事-如果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
    

    编辑
    如果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是并且自动递增(标识列)时,才执行此操作

    10-05 17:44
    查看更多