I want to add 12 players in 1 transaction only. My codes here can only save 1 player.
Here's my codes:

Private Sub savebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles savebtn.Click
    Dim conn As MySqlConnection
    Dim myCommand As New MySqlCommand
    Dim commandB As MySqlCommandBuilder
    Dim data As MySqlDataAdapter
    Dim query As String = "SELECT * FROM basketplayer b;"
    Dim myconnstring As String
    Dim comm As String

        Dim value As String
        value = TeamNameTextBox.Text & "', '" & Player1NumberTextBox.Text & "', '" & Player1NameTextBox.Text

'11 more textboxes that contains players

        myconnstring = "datasource=localhost;username=root;password=monchito;database=newscoring;"
        comm = "INSERT INTO `basketplayer` (`team`, `plnum`, `playernam`, `pts`, `throw`, `foul`) VALUES ('" & value & "', '0', '0', '0');"
        conn = New MySqlConnection(myconnstring)

            data = New MySqlDataAdapter(query, conn)
            commandB = New MySqlCommandBuilder(data)
            Dim res As New MySqlParameter
            res.Value = commandB
            myCommand.CommandText = comm
            myCommand.Connection = conn

'1 Player that has saved in this command. Only the first entry.

                MessageBox.Show("Players successfully added!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
                newbtn.PerformClick() 'all textbox are cleared
            Catch myerror As MySqlException
                MessageBox.Show("Error Connecting to Database: " & myerror.Message)
            End Try
        Catch myerror As MySqlException
            MessageBox.Show("Error Connecting to Database: " & myerror.Message)
        End Try
End Sub

Although there are numerous ways to insert multiple records in a database, the easiest way is this:


well it is better to first add all the records of your all players in a datagrid then insert them in db by using loop . if you are not able to use datagrid then use datatable , insert all records in it then then save them using loop.

If you have query regarding above mentioned methods ,Please Post your question here

Best Regards

I am using vb.net, Is that vb? I was thinking that is PHP.

Hi monching,

It's an SQL statement. Multiple insertion in one execution.

Though, you can use .Net's stringbuilder like this one, to execute the above mentioned command: Sample

Thanks gon,
It works!

Hi M.Wagas Aslam,
I prefer to use gon's suggestion, because as what he have said it is easier. Thank you for your concern.

Walang anu man. Anytime.

Hi GON, pa help nman. Meron akong Invoice number, Fname, Lname, at datagridview with multiple rows and column. (Product ID, Price, Qty, Amount ). Gusto ko sana i-save within 1 record. Unique value is Invoice num. Mysql gamit ko

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.