Learning foreach...
-
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(); }
-
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(); }
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!
-
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(); }
JollyMansArt wrote:
- 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