Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Get number of records affected in SQL Update

Posted on 2014-10-10
3
Medium Priority
?
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

721 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