1

如何获取消息框以返回作为更新和插入的记录数。这是我到目前为止提出的代码,但问题在于此代码仅返回值 1。奇怪的是,当我插入 5 条记录时,messagbox 出现 5 次但值为 1。

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button2.Click
    Dim i As Integer = 0
    Dim student_id, name ,age dbSQL_query As String
    Dim dbSQL_cmd As New SqlClient.SqlCommand
    Dim dbSQL_con As New SqlClient.SqlConnection

    'dbSQL_con.ConnectionString = "Server=*****;Database=***;Uid=****;Pwd=*****;"
    'dbSQL_con.Open()
    Using SQLConnection As New MySqlConnection(connectionString)
        SQLConnection.Open()
        For i = 0 To DataGridView1.RowCount - 1
            Using sqlCommand As New MySqlCommand()
                student_id = DataGridView1.Item(0, i).Value.ToString
                name = DataGridView1.Item(1, i).Value.ToString
                age = DataGridView1.Item(2, i).Value.ToString


                dbSQL_query = "INSERT INTO students (student_id, name, age )  VALUES  (" + addQoute(student_id) + ", " + _
                      addQoute(name) + ", " + _ addQoute(age) + ") on duplicate key update student_id=(" + addQoute(student_id) + "),name=(" + addQoute(name) + "),age=(" + addQoute(age) + ")"
                With sqlCommand

                    .CommandText = dbSQL_query
                    .Connection = SQLConnection
                    .CommandType = CommandType.Text
                End With
                sqlCommand.ExecuteNonQuery()
                dbSQL_cmd = New SqlClient.SqlCommand(dbSQL_query, dbSQL_con)

            End Using

        Next
        'MessageBox.Show(DataGridView1.RowCount & " bonds have been inserted")
        SQLConnection.Close()
    End Using
End Sub

Public Function addQoute(ByVal str As String) As String
    str = Trim(str)
    Return "'" + str + "'"
End Function
4

0 回答 0