简体   繁体   中英

How to sum datagridview contents to database VB.net multiple users

How can I sum the contents of my datagridview on my database. i have a code shown, however it just updates not sum.

So here is my code on save:

Public Sub save_dgv()
        Try
            con.Close()
            con.Open()
            Dim scb = New MySqlCommandBuilder(SDA)
            SDA.Update(dbDataset)
            MessageBox.Show("Submitted!")
            con.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


sample:

user1
id--row1--row2--total
1 --25  -- 23 --48

user2
id--row1--row2--total
1 --25  -- 23 --48

output
id--row1--row2--total
1 --50-- 46--96

Try the following. Adjust data types to your db. Works in SQL Server but not tested in MySQL.

Using cn As New MySqlConnection("Your connection string")
            Dim s As String = "Update MyTable Set row1 = row1 + @row1, row2 = row2 + @row2 where id = @id"
            Using cmd As New MySqlCommand(s, cn)
                cmd.Parameters.Add("@row1", MySqlDbType.Int32).Value = intRow1
                cmd.Parameters.Add("@row2", MySqlDbType.Int32).Value = intRow2
                cmd.Parameters.Add("@id", MySqlDbType.Int32).Value = intID
                cn.Open()
                cmd.ExecuteNonQuery()
            End Using
End Using

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM