更新と挿入として入ってくるレコードの数を返すメッセージボックスを取得するにはどうすればよいですか? ここに私がこれまでに思いついたコードがありますが、問題はこのコードで1の値しか返さないことです.5つのレコードを挿入すると、メッセージボックスが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