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 2 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

    I would like to learn how to do for each. I have been researching. I also know this should be one of those no brainier questions but I am still piecing together how to make foreach for for me. I know how to count my tables and fields then place the information in. But what I would like is to learn how to use the foreach command. to say something like. Foreach (table in Database) { Foreach (column in table) { } } instead of counting the tables and columns and their names. This will allow me to recreate a database from an existing one. Any websites or information you can provide to point me in the right direction would be appreciated. Just for reference purposes...

    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();
               }
    
    V 1 Reply Last reply
    0
    • J JollyMansArt

      I would like to learn how to do for each. I have been researching. I also know this should be one of those no brainier questions but I am still piecing together how to make foreach for for me. I know how to count my tables and fields then place the information in. But what I would like is to learn how to use the foreach command. to say something like. Foreach (table in Database) { Foreach (column in table) { } } instead of counting the tables and columns and their names. This will allow me to recreate a database from an existing one. Any websites or information you can provide to point me in the right direction would be appreciated. Just for reference purposes...

      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();
                 }
      
      V Offline
      V Offline
      VanityClaw
      wrote on last edited by
      #2

      Not to poo-poo your question, but why are you writing code to do what sql already can? Anyway - I'm not sure I actually get your question. If you're looking for the db/table/column structure info, you can use queries (which I'll inlcude at the end of this post) - but I'd try looking at the Interop.SQLDMO objects. Slow & a bit old-fasioned (I'm sure) but it works (I use it in a code generator I made) pretty well. Aforementioned scripts: -- select * From information_schema.tables -- (Tables) -- select * From information_schema.columns -- (Columns) -- Select * From syscolumns -- (another way to look at columns) -- sp_fkeys([table] -- (fkeys for table) -- SELECT FK.Table_name 'FKTableName', CU.column_name 'FKColumnName', pk.table_name 'PKTableName', pt.column_name 'PKColumnName',cu.table_catalog FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS C INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME INNER JOIN (SELECT i1.TABLE_NAME, i2.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS i1 INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME WHERE (i1.CONSTRAINT_TYPE = 'PRIMARY KEY')) AS PT ON PT.TABLE_NAME = PK.TABLE_NAME where fk.table_name = '[table]' -- (Pkeys for table) You're on your own with indexes and constraints (I'm not fiddling with that in my code generator). Does that help, or did I miss what you're asking altogether?

      J 1 Reply Last reply
      0
      • V VanityClaw

        Not to poo-poo your question, but why are you writing code to do what sql already can? Anyway - I'm not sure I actually get your question. If you're looking for the db/table/column structure info, you can use queries (which I'll inlcude at the end of this post) - but I'd try looking at the Interop.SQLDMO objects. Slow & a bit old-fasioned (I'm sure) but it works (I use it in a code generator I made) pretty well. Aforementioned scripts: -- select * From information_schema.tables -- (Tables) -- select * From information_schema.columns -- (Columns) -- Select * From syscolumns -- (another way to look at columns) -- sp_fkeys([table] -- (fkeys for table) -- SELECT FK.Table_name 'FKTableName', CU.column_name 'FKColumnName', pk.table_name 'PKTableName', pt.column_name 'PKColumnName',cu.table_catalog FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS C INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME INNER JOIN (SELECT i1.TABLE_NAME, i2.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS i1 INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME WHERE (i1.CONSTRAINT_TYPE = 'PRIMARY KEY')) AS PT ON PT.TABLE_NAME = PK.TABLE_NAME where fk.table_name = '[table]' -- (Pkeys for table) You're on your own with indexes and constraints (I'm not fiddling with that in my code generator). Does that help, or did I miss what you're asking altogether?

        J Offline
        J Offline
        JollyMansArt
        wrote on last edited by
        #3

        I have a quickbooks database that I need to replicate into sql server. The quickbooks database I have an ODBC connection to. But inorder to port the data from quickbook to sql server I have to generate the tables in sql server as they are in the quickbooks database. Then I can port the data from quickbooks to sql Server. SQL Server has no odbc connection to quickbooks otherwise I agree with you. I have found a piece of code that might help but I currently do not understand it. http://stackoverflow.com/questions/47239/how-can-i-generate-database-tables-from-c-classes[^]

        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