Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Data Query

Data Query

Scheduled Pinned Locked Moved C#
databasehelpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sean89
    wrote on last edited by
    #1

    Can someone tell me why this is returning 0?

            static void Main(string[] args)
            {
                string dataPath = @"C:\Documents and Settings\Administrator\Desktop\Transimount_Data.MDB";
                OleDbConnection conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0; data source=" + dataPath);
    
                OleDbCommand cmd = new OleDbCommand("SELECT * FROM Mounts WHERE Group_Number = 1", conn);
    
                conn.Open();
                int count = cmd.ExecuteNonQuery();
                conn.Close();
    
                Console.WriteLine(count); 
            }
    

    I am using this to get how many records are in Group 1 of a collection of parts. The datatable in the database currently looks like this:

    Mount_Number	Carrier_Part_Number	Group_Number
    KMC84191	72-62039-02	          5
    KMK64191	72-62039-00	          3
    KMK71074	72-62030-aa	          1
    KMK71374	72-62030-bb	          1
    KMK72073	72-62030-cc	          1
    KMK72673	72-62030-dd	          1
    KMM61901	72-62038-03	          6
    KMM61990	72-62038-05	          2
    KMM62600	72-62038-04	          6
    KMM62690	72-62038-02	          2
    KMM64191	72-62039-01	          4
    

    I'm a little tired so I hope this is a stupid mistake ;) Thanks for any help!

    K K 2 Replies Last reply
    0
    • S Sean89

      Can someone tell me why this is returning 0?

              static void Main(string[] args)
              {
                  string dataPath = @"C:\Documents and Settings\Administrator\Desktop\Transimount_Data.MDB";
                  OleDbConnection conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0; data source=" + dataPath);
      
                  OleDbCommand cmd = new OleDbCommand("SELECT * FROM Mounts WHERE Group_Number = 1", conn);
      
                  conn.Open();
                  int count = cmd.ExecuteNonQuery();
                  conn.Close();
      
                  Console.WriteLine(count); 
              }
      

      I am using this to get how many records are in Group 1 of a collection of parts. The datatable in the database currently looks like this:

      Mount_Number	Carrier_Part_Number	Group_Number
      KMC84191	72-62039-02	          5
      KMK64191	72-62039-00	          3
      KMK71074	72-62030-aa	          1
      KMK71374	72-62030-bb	          1
      KMK72073	72-62030-cc	          1
      KMK72673	72-62030-dd	          1
      KMM61901	72-62038-03	          6
      KMM61990	72-62038-05	          2
      KMM62600	72-62038-04	          6
      KMM62690	72-62038-02	          2
      KMM64191	72-62039-01	          4
      

      I'm a little tired so I hope this is a stupid mistake ;) Thanks for any help!

      K Offline
      K Offline
      Kayess Tech
      wrote on last edited by
      #2

      Hi Sean, If all you want the the count then just change your SQL to SELECT COUNT(*) FROM Mounts WHERE Group_Number = 1 HTH Web design and hosting http://www.kayess.com.au

      S 1 Reply Last reply
      0
      • K Kayess Tech

        Hi Sean, If all you want the the count then just change your SQL to SELECT COUNT(*) FROM Mounts WHERE Group_Number = 1 HTH Web design and hosting http://www.kayess.com.au

        S Offline
        S Offline
        Sean89
        wrote on last edited by
        #3

        Thanks for the response! I tried it and it still seems to be returning 0. Even if I change the query to select everything: SELECT * FROM Mounts It still seems to say that the number of rows affected is 0. I'll just use this till I decide to figure out whats wrong:

                    OleDbCommand cmd = new OleDbCommand("SELECT COUNT(*) FROM Groups", conn);
        
                    conn.Open();
        
                    OleDbDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        int count = reader[0];
                    }
        
                    conn.Close(); 
        

        It works at least ;P

        R 1 Reply Last reply
        0
        • S Sean89

          Thanks for the response! I tried it and it still seems to be returning 0. Even if I change the query to select everything: SELECT * FROM Mounts It still seems to say that the number of rows affected is 0. I'll just use this till I decide to figure out whats wrong:

                      OleDbCommand cmd = new OleDbCommand("SELECT COUNT(*) FROM Groups", conn);
          
                      conn.Open();
          
                      OleDbDataReader reader = cmd.ExecuteReader();
                      while (reader.Read())
                      {
                          int count = reader[0];
                      }
          
                      conn.Close(); 
          

          It works at least ;P

          R Offline
          R Offline
          Rob Graham
          wrote on last edited by
          #4

          Since your query changed no rows, the rows affected will be 0... You can't get a rowcount from a datareader until you read to the end. The result of your query IS a single record containing a single column whose value is the number of records matching the query. I wouldn't know how else to get it than he way you settled on... We need to graduate from the ridiculous notion that greed is some kind of elixir for capitalism - it's the downfall of capitalism. Self-interest, maybe, but self-interest run amok does not serve anyone. The core value of conscious capitalism is enlightened self-interest. Patricia Aburdene Bulls make money, bears make money, pigs get slaughtered. Jim Cramer

          S 1 Reply Last reply
          0
          • R Rob Graham

            Since your query changed no rows, the rows affected will be 0... You can't get a rowcount from a datareader until you read to the end. The result of your query IS a single record containing a single column whose value is the number of records matching the query. I wouldn't know how else to get it than he way you settled on... We need to graduate from the ridiculous notion that greed is some kind of elixir for capitalism - it's the downfall of capitalism. Self-interest, maybe, but self-interest run amok does not serve anyone. The core value of conscious capitalism is enlightened self-interest. Patricia Aburdene Bulls make money, bears make money, pigs get slaughtered. Jim Cramer

            S Offline
            S Offline
            Sean89
            wrote on last edited by
            #5

            Thats what I thought too, but for some reason I thought I had done it with ExecuteNonQuery(); before... Guess not ;P Thanks for the help.

            1 Reply Last reply
            0
            • S Sean89

              Can someone tell me why this is returning 0?

                      static void Main(string[] args)
                      {
                          string dataPath = @"C:\Documents and Settings\Administrator\Desktop\Transimount_Data.MDB";
                          OleDbConnection conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0; data source=" + dataPath);
              
                          OleDbCommand cmd = new OleDbCommand("SELECT * FROM Mounts WHERE Group_Number = 1", conn);
              
                          conn.Open();
                          int count = cmd.ExecuteNonQuery();
                          conn.Close();
              
                          Console.WriteLine(count); 
                      }
              

              I am using this to get how many records are in Group 1 of a collection of parts. The datatable in the database currently looks like this:

              Mount_Number	Carrier_Part_Number	Group_Number
              KMC84191	72-62039-02	          5
              KMK64191	72-62039-00	          3
              KMK71074	72-62030-aa	          1
              KMK71374	72-62030-bb	          1
              KMK72073	72-62030-cc	          1
              KMK72673	72-62030-dd	          1
              KMM61901	72-62038-03	          6
              KMM61990	72-62038-05	          2
              KMM62600	72-62038-04	          6
              KMM62690	72-62038-02	          2
              KMM64191	72-62039-01	          4
              

              I'm a little tired so I hope this is a stupid mistake ;) Thanks for any help!

              K Offline
              K Offline
              Kuira
              wrote on last edited by
              #6

              Try this:

              static void Main(string[] args)
              {
                 string dataPath = @"C:\Documents and Settings\Administrator\Desktop\Transimount_Data.MDB";
                 OleDbConnection conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0; data source=" + dataPath);
              
                 OleDbCommand cmd = new OleDbCommand("SELECT COUNT(*) FROM Mounts WHERE Group_Number = 1", conn);
              
                 conn.Open();
                 int count = cmd.ExecuteScalar();
                 conn.Close();
              
                 Console.WriteLine(count); 
              }
              

              Kuira

              S 1 Reply Last reply
              0
              • K Kuira

                Try this:

                static void Main(string[] args)
                {
                   string dataPath = @"C:\Documents and Settings\Administrator\Desktop\Transimount_Data.MDB";
                   OleDbConnection conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0; data source=" + dataPath);
                
                   OleDbCommand cmd = new OleDbCommand("SELECT COUNT(*) FROM Mounts WHERE Group_Number = 1", conn);
                
                   conn.Open();
                   int count = cmd.ExecuteScalar();
                   conn.Close();
                
                   Console.WriteLine(count); 
                }
                

                Kuira

                S Offline
                S Offline
                Sean89
                wrote on last edited by
                #7

                Yeah that works :) Thanks!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups