Solved

SQL Get number of records affected in SQL Update

Posted on 2014-10-10
3
290 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 51

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 70

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:Murray Brown
ID: 40372933
Thank you very much
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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 to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

623 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