Solved

SQL Get number of records affected in SQL Update

Posted on 2014-10-10
3
253 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:murbro
3 Comments
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 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 69

Accepted Solution

by:
Éric Moreau earned 250 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:murbro
ID: 40372933
Thank you very much
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now