Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Get number of records affected in SQL Update

Posted on 2014-10-10
3
Medium Priority
?
306 Views
Last Modified: 2014-10-10
Hi
    I use several SQL update statements such the one below in my VB.net project.
    How do I get the number of records affected by each update statement?

     Dim sSQL As String

            sSQL = "Update [" & Me.lblCurrentTable.Text & "] Set Category1 = '" & Me.cmbCategory1.Text & "'"
            sSQL = sSQL & " Where [Product ID] = '" & oProductID & "'"

            Dim cn As New SqlConnection(My.Settings.CS)
            Dim cmd As New SqlCommand(sSQL, cn)

            cn.Open()
            cmd.ExecuteNonQuery()
            cn.Close()
0
Comment
Question by:Murray Brown
3 Comments
 
LVL 52

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 1000 total points
ID: 40372387
If you run the SQLCommand with an output variable, you'll have the number of affected records:
cmd.Execute(@AffectedRecords)

Open in new window

0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1000 total points
ID: 40372616
as shown in http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx, the ExecuteNonQuery returns an integer indicating the number of rows affected.

dim r as integer = cmd.ExecuteNonQuery()
0
 

Author Closing Comment

by:Murray Brown
ID: 40372933
Thank you very much
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question