Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find out how many rows are returned as int, if not 0, if there is a result returned by the query.

What I have tried:

using (var except=new SqlCommand("SELECT ID FROM STUDENT EXCEPT SELECT ID FROM REGISTERSTUDENT),connection)
{
  if(except !=0) // I want the integer version of except. But the sql command doesn't come as int.
{
   using(var add=new SqlCommand("INSERT FROM REGISTERSTUDENT (SELECT ID FROM STUDENT EXCEPT SELECT ID FROM REGISTERSTUDENT))
{
    add.ExecuteNonQuery();
}
} 
}
Posted
Updated 22-Jul-19 11:00am

1 solution

Declaring a new SqlCommand will return just that, a command. What you want is the results of executing that command, and that is via the ExecuteNonQuery method.

But you will need more than this- your code probably won't even compile as the command you define in line #1 doesn't have the string closed, and your parenthesis are broken too. Try formatting your code, and declaring the language type when you are pasting it in and you would noticed it's boogered-up

C#
<pre>using (SqlCommand cmd = new SqlCommand("SELECT ID FROM STUDENT EXCEPT SELECT ID FROM REGISTERSTUDENT", connection)) {
  
  // if connection not already open ==> Open It
  int except = cmd.ExecuteNonQuery();

  if (except != 0) // continue on
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900