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. Learning foreach...

Learning foreach...

Scheduled Pinned Locked Moved C#
databasecomhtmlsql-serversysadmin
3 Posts 3 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.
  • J Offline
    J Offline
    JollyMansArt
    wrote on last edited by
    #1

    What I would like to do is instead of manually parsing for each table and row. I would like to learn how to do a foreach to dynamically find each table and each row in a database. Here is what I curently have, All I am asking is how to use the foreach command to do this. 1) How would you say: foreach(table in database) then how would you say foreach(row in table) New experimental project I am playing with. I will post back when I find something. Just thought I would ask.

    string sConnectString = "Driver={QODBC Driver for QuickBooks};DFQ=C:\\Quickbooks\\sample_company_file.qbw;OpenMode=M;OLE DB Services=-2;";
    string sSQL = "SELECT Name FROM Employee";

            OdbcConnection cn;
            OdbcCommand cmd;
            
            //string MyString;
            //MyString="Select \* from Customers";
            //cn = new OdbcConnection("Driver={SQL Server};Server=mySQLServer;UID=sa;PWD=myPassword;Database=Northwind;");
            
            cn = new OdbcConnection(sConnectString);
            cmd =new OdbcCommand(sSQL,cn);
            
            
            //cn.Open();
            //MessageBox.Show("Connected");
            //cn.Close();
            
            try               
               {
                  cn.Open();
                MessageBox.Show("open");
                //odbcconnection1.GetSchema("tables")
                //odbcconnection1.GetSchema("columns")
                //http://www.daniweb.com/forums/thread194354.html
                OdbcDataReader dr = cmd.ExecuteReader();
    
                dr.GetSchemaTable().
                MessageBox.Show(dr.GetSchemaTable().TableName);
    
    
    
    
    
               }
            catch (OdbcException ex)
               {
                  MessageBox.Show(ex.Message);//<BR/>   There should be no <BR/>
               }
            finally
               {
                   cn.Close();
               }
    
    L OriginalGriffO 2 Replies Last reply
    0
    • J JollyMansArt

      What I would like to do is instead of manually parsing for each table and row. I would like to learn how to do a foreach to dynamically find each table and each row in a database. Here is what I curently have, All I am asking is how to use the foreach command to do this. 1) How would you say: foreach(table in database) then how would you say foreach(row in table) New experimental project I am playing with. I will post back when I find something. Just thought I would ask.

      string sConnectString = "Driver={QODBC Driver for QuickBooks};DFQ=C:\\Quickbooks\\sample_company_file.qbw;OpenMode=M;OLE DB Services=-2;";
      string sSQL = "SELECT Name FROM Employee";

              OdbcConnection cn;
              OdbcCommand cmd;
              
              //string MyString;
              //MyString="Select \* from Customers";
              //cn = new OdbcConnection("Driver={SQL Server};Server=mySQLServer;UID=sa;PWD=myPassword;Database=Northwind;");
              
              cn = new OdbcConnection(sConnectString);
              cmd =new OdbcCommand(sSQL,cn);
              
              
              //cn.Open();
              //MessageBox.Show("Connected");
              //cn.Close();
              
              try               
                 {
                    cn.Open();
                  MessageBox.Show("open");
                  //odbcconnection1.GetSchema("tables")
                  //odbcconnection1.GetSchema("columns")
                  //http://www.daniweb.com/forums/thread194354.html
                  OdbcDataReader dr = cmd.ExecuteReader();
      
                  dr.GetSchemaTable().
                  MessageBox.Show(dr.GetSchemaTable().TableName);
      
      
      
      
      
                 }
              catch (OdbcException ex)
                 {
                    MessageBox.Show(ex.Message);//<BR/>   There should be no <BR/>
                 }
              finally
                 {
                     cn.Close();
                 }
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, 1. obviously you get all rows in a table by selecting everything as in "SELECT * FROM tablename", so just skip the WHERE clause. 2. how you get information on the tables themselves probably depends on the database; in MySQL it simply is "SHOW TABLES" Look in the documentation for your database! :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      Local announcement (Antwerp region): Lange Wapper? Neen!


      1 Reply Last reply
      0
      • J JollyMansArt

        What I would like to do is instead of manually parsing for each table and row. I would like to learn how to do a foreach to dynamically find each table and each row in a database. Here is what I curently have, All I am asking is how to use the foreach command to do this. 1) How would you say: foreach(table in database) then how would you say foreach(row in table) New experimental project I am playing with. I will post back when I find something. Just thought I would ask.

        string sConnectString = "Driver={QODBC Driver for QuickBooks};DFQ=C:\\Quickbooks\\sample_company_file.qbw;OpenMode=M;OLE DB Services=-2;";
        string sSQL = "SELECT Name FROM Employee";

                OdbcConnection cn;
                OdbcCommand cmd;
                
                //string MyString;
                //MyString="Select \* from Customers";
                //cn = new OdbcConnection("Driver={SQL Server};Server=mySQLServer;UID=sa;PWD=myPassword;Database=Northwind;");
                
                cn = new OdbcConnection(sConnectString);
                cmd =new OdbcCommand(sSQL,cn);
                
                
                //cn.Open();
                //MessageBox.Show("Connected");
                //cn.Close();
                
                try               
                   {
                      cn.Open();
                    MessageBox.Show("open");
                    //odbcconnection1.GetSchema("tables")
                    //odbcconnection1.GetSchema("columns")
                    //http://www.daniweb.com/forums/thread194354.html
                    OdbcDataReader dr = cmd.ExecuteReader();
        
                    dr.GetSchemaTable().
                    MessageBox.Show(dr.GetSchemaTable().TableName);
        
        
        
        
        
                   }
                catch (OdbcException ex)
                   {
                      MessageBox.Show(ex.Message);//<BR/>   There should be no <BR/>
                   }
                finally
                   {
                       cn.Close();
                   }
        
        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        JollyMansArt wrote:

        1. How would you say: foreach(table in database) then how would you say foreach(row in table)

        foreach is not a realy usable construct for examining database tables, I would normally use

                reader = sqlCmd.ExecuteReader();
                while (reader.Read())
                    {
                    ...
                    }
        

        Think about it: if you were dealing with a file, you would say:

        string[] lines = inFile.ReadAllLines();
        foreach(string s in lines)
        {
        ...
        }

        rather than trying:

        foreach(LineOfText line in inFile)
        {
        ...
        }

        You can't use foreach for the columns, either - there isn't an Items collection - but even if you could, it really wouldn't be too helpfull:

        foreach(var v in reader.Items)
        {
        ... Workout what type it is and what to do with it...
        }

        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        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