Need Oracle Column Names
-
Hello, I am trying to return the values of the column names from a table in an Oracle 10g database. I have Googled this "ad-nausium" and have found some code which seems to be most preferred:
select column_name from all_tab_columns where table_name = 'TABLE_NAME' order by column_id"
However, this returns the Column Header Stamp "Column Name" for me instead, and I have seen others reply with this same issue; I have not yet found anyone that can explain or follow this up with a correction. I certainly can not figure it out. The complete context code is below as follows:
listViewColumns.Items.Clear();
try
{
//Get the Cols for the selected table
OleDbDataAdapter da = new OleDbDataAdapter(Sql, Conn);
DataTable dt = new DataTable();
da.Fill(dt);foreach (object col in dt.Columns)
{
listViewColumns.Items.Add(col.ToString());
}
}For background, the program is a C# 4.0 Framwork application and I am using an OLEDB provider for Oracle. Some assistance would be greatly appreciated. Thank You, Pat
Hi, I don't know for Oracle, however I did similar things with OLEDB for Access, basically it consisted of: - opening a connection - creating an OleDbCommand with the appropriate SQL, probably SELECT * FROM tableName - getting a OleDbDataReader by executing OleDbCommand.ExecuteReader (CommandBehavior.SchemaOnly should suffice); - getting the table's schema from OleDbDataReader.GetSchemaTable. - cleaning up. The schema itself is a DataTable that holds all the field information. You'll have to work out the details and try it on Oracle. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Hello, I am trying to return the values of the column names from a table in an Oracle 10g database. I have Googled this "ad-nausium" and have found some code which seems to be most preferred:
select column_name from all_tab_columns where table_name = 'TABLE_NAME' order by column_id"
However, this returns the Column Header Stamp "Column Name" for me instead, and I have seen others reply with this same issue; I have not yet found anyone that can explain or follow this up with a correction. I certainly can not figure it out. The complete context code is below as follows:
listViewColumns.Items.Clear();
try
{
//Get the Cols for the selected table
OleDbDataAdapter da = new OleDbDataAdapter(Sql, Conn);
DataTable dt = new DataTable();
da.Fill(dt);foreach (object col in dt.Columns)
{
listViewColumns.Items.Add(col.ToString());
}
}For background, the program is a C# 4.0 Framwork application and I am using an OLEDB provider for Oracle. Some assistance would be greatly appreciated. Thank You, Pat
I have the script in the office that gets the table names of tables and views - if you don't have an answer I'll post it tomorrow.
Never underestimate the power of human stupidity RAH
-
I have the script in the office that gets the table names of tables and views - if you don't have an answer I'll post it tomorrow.
Never underestimate the power of human stupidity RAH
-
Hi, I don't know for Oracle, however I did similar things with OLEDB for Access, basically it consisted of: - opening a connection - creating an OleDbCommand with the appropriate SQL, probably SELECT * FROM tableName - getting a OleDbDataReader by executing OleDbCommand.ExecuteReader (CommandBehavior.SchemaOnly should suffice); - getting the table's schema from OleDbDataReader.GetSchemaTable. - cleaning up. The schema itself is a DataTable that holds all the field information. You'll have to work out the details and try it on Oracle. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Thank you for your input Luc. I recall that you have supplied me with some other solutions in times past and I appreciate your input. I will try your scripting later or tomorrow as time permits and post it here if it works. Thanks again...Pat
You're welcome. In the mean time I found a matching code snippet:
using (OleDbConnection con=new OleDbConnection(connectionString)) {
con.Open();
using (OleDbCommand cmd=new OleDbCommand("SELECT * FROM ["+tableName+"]", con)) {
// ref: http://www.pcreview.co.uk/forums/getschema-columns-results-t2877051.html
DbDataReader rdr=cmd.ExecuteReader(CommandBehavior.SchemaOnly);
DataTable schema=rdr.GetSchemaTable();
foreach (DataColumn col in schema.Columns) log("colName="+col.ColumnName);
}
}:)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Hello, I am trying to return the values of the column names from a table in an Oracle 10g database. I have Googled this "ad-nausium" and have found some code which seems to be most preferred:
select column_name from all_tab_columns where table_name = 'TABLE_NAME' order by column_id"
However, this returns the Column Header Stamp "Column Name" for me instead, and I have seen others reply with this same issue; I have not yet found anyone that can explain or follow this up with a correction. I certainly can not figure it out. The complete context code is below as follows:
listViewColumns.Items.Clear();
try
{
//Get the Cols for the selected table
OleDbDataAdapter da = new OleDbDataAdapter(Sql, Conn);
DataTable dt = new DataTable();
da.Fill(dt);foreach (object col in dt.Columns)
{
listViewColumns.Items.Add(col.ToString());
}
}For background, the program is a C# 4.0 Framwork application and I am using an OLEDB provider for Oracle. Some assistance would be greatly appreciated. Thank You, Pat
Another trouble: if there are more than one table visible with thesame name (in other schemas for instance) you got mixed output from those tables. Try use
user_tab_columns
instead or add next condition to WHERE clauseAND owner = 'myowner'
where 'myowner' is owner name.
--- AW
-
Hello, I am trying to return the values of the column names from a table in an Oracle 10g database. I have Googled this "ad-nausium" and have found some code which seems to be most preferred:
select column_name from all_tab_columns where table_name = 'TABLE_NAME' order by column_id"
However, this returns the Column Header Stamp "Column Name" for me instead, and I have seen others reply with this same issue; I have not yet found anyone that can explain or follow this up with a correction. I certainly can not figure it out. The complete context code is below as follows:
listViewColumns.Items.Clear();
try
{
//Get the Cols for the selected table
OleDbDataAdapter da = new OleDbDataAdapter(Sql, Conn);
DataTable dt = new DataTable();
da.Fill(dt);foreach (object col in dt.Columns)
{
listViewColumns.Items.Add(col.ToString());
}
}For background, the program is a C# 4.0 Framwork application and I am using an OLEDB provider for Oracle. Some assistance would be greatly appreciated. Thank You, Pat
I think I found out your problem. Your select statement is correct and should return the names of all columns in your table. You can verify this by running the query in a client tool like SQL * Plus or TOAD. It's how you bind the data table to the ListView that makes the difference. Instead of querying the DataTable's columns, you should query its rows because the column information is returned as rows. Try this:
foreach (DataRow row dt.Rows) {
listViewColumns.Items.Add(row[0].ToString());
}modified on Sunday, August 28, 2011 6:22 AM
-
Hello, I am trying to return the values of the column names from a table in an Oracle 10g database. I have Googled this "ad-nausium" and have found some code which seems to be most preferred:
select column_name from all_tab_columns where table_name = 'TABLE_NAME' order by column_id"
However, this returns the Column Header Stamp "Column Name" for me instead, and I have seen others reply with this same issue; I have not yet found anyone that can explain or follow this up with a correction. I certainly can not figure it out. The complete context code is below as follows:
listViewColumns.Items.Clear();
try
{
//Get the Cols for the selected table
OleDbDataAdapter da = new OleDbDataAdapter(Sql, Conn);
DataTable dt = new DataTable();
da.Fill(dt);foreach (object col in dt.Columns)
{
listViewColumns.Items.Add(col.ToString());
}
}For background, the program is a C# 4.0 Framwork application and I am using an OLEDB provider for Oracle. Some assistance would be greatly appreciated. Thank You, Pat
As Luc said, use a DataReader; it always* works, no matter the database system**, and you can use it for queries as well as tables. Plus it can tell you the proper .net datatype that maps to the table column. :thumbsup: * Well, I haven't yet seen a situation in which it doesn't. ** I haven't tried it with XML, but I don't consider that a database system. I suppose, if this doesn't work, then the target must not be a database system. :-D
-
I think I found out your problem. Your select statement is correct and should return the names of all columns in your table. You can verify this by running the query in a client tool like SQL * Plus or TOAD. It's how you bind the data table to the ListView that makes the difference. Instead of querying the DataTable's columns, you should query its rows because the column information is returned as rows. Try this:
foreach (DataRow row dt.Rows) {
listViewColumns.Items.Add(row[0].ToString());
}modified on Sunday, August 28, 2011 6:22 AM
YES YES YES...Thank you, that did it! I really need to have a better understanding of what is really going on in these ADO calls. Now I see the column information better since you explained that the columns are returned as rows. I have posted my final code below for your perusal and for the many others that I have accidentally found while trying to resolve this. Thanks again...Pat
string Sql = "select column_name from all_tab_columns where table_name = '" + sSelectedTable + "' order by column_id";
OleDbCommand Comm = new OleDbCommand(Sql, Conn);listViewColumns.Items.Clear(); try { //List the schema info for the selected table OleDbDataAdapter da = new OleDbDataAdapter(Sql, Conn); DataTable dt = new DataTable(); da.Fill(dt); foreach (DataRow row in dt.Rows) { listViewColumns.Items.Add(row\[0\].ToString()); } }
-
You're welcome. In the mean time I found a matching code snippet:
using (OleDbConnection con=new OleDbConnection(connectionString)) {
con.Open();
using (OleDbCommand cmd=new OleDbCommand("SELECT * FROM ["+tableName+"]", con)) {
// ref: http://www.pcreview.co.uk/forums/getschema-columns-results-t2877051.html
DbDataReader rdr=cmd.ExecuteReader(CommandBehavior.SchemaOnly);
DataTable schema=rdr.GetSchemaTable();
foreach (DataColumn col in schema.Columns) log("colName="+col.ColumnName);
}
}:)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Luc, A minute of my time to thank you for your time. I received this solution (below) from Mr Holmes that is short and sweet. I am sure that if I had a better understanding of the "schema" calls, this would have worked as well, but when I ran it, it placed all the schema headings into the listview. I am sure there is some small argument that would fix it, and I should look nto it as this might help with some other calls. Thank you for your help...I hope that you will answer my 'other issues' as they come up. I appreciate your time and skill...Pat
-
As Luc said, use a DataReader; it always* works, no matter the database system**, and you can use it for queries as well as tables. Plus it can tell you the proper .net datatype that maps to the table column. :thumbsup: * Well, I haven't yet seen a situation in which it doesn't. ** I haven't tried it with XML, but I don't consider that a database system. I suppose, if this doesn't work, then the target must not be a database system. :-D
-
Thank you...I have worked with Luc before and he has helped me a lot, but the solution that worked perfectly this time is posted above by Mr Holmes. I thought you might want to note it for future use. Thank you for your input..Best Regards, Pat
-
Thank you...I have worked with Luc before and he has helped me a lot, but the solution that worked perfectly this time is posted above by Mr Holmes. I thought you might want to note it for future use. Thank you for your input..Best Regards, Pat
No, I most certainly do not; I very much prefer a database-agnostic solution when one is available and especially when such is better (more flexible, more informative) than the database-specific one.
-
Thank You RAH, I am going to try Luc's Schema solution later and see if it works, but I would still like to have a look at your proven solution. A post tomorrow, as well as your reply today, is much appreciated. Thank You, Pat
Not sure if this is relevant anymore but here is the code that gets the tables and views from an oracle database. DBOpsO creates a valid oracle connection and executes a sql string to return a datatable.
StringBuilder sSQL = new StringBuilder(); TableDB oTableDB; sSQL.AppendLine("select T.owner,T.table\_name, NVL (V.HasView, 0)HasView"); sSQL.AppendLine("From SYS.ALL\_TABLES T "); sSQL.AppendLine("left join (select owner,view\_name, 1 HasView from SYS.ALL\_VIEWS where owner in ('SchemaName1','SchemaName2')) V on upper(V.View\_Name) = 'VW' || T.table\_name "); sSQL.AppendFormat("and V.owner = T.owner").AppendLine(); sSQL.AppendFormat("where T.owner in ({0})", sSchema.ToString()).AppendLine(); sSQL.AppendLine("order by table\_name"); if (oDBOpsO == null || oDBOpsO.Creds.Database != oDB.DatabaseName) { oDBOpsO = clsMain.GetDBOpsO(oDB.Server.ServerName, oDB.DatabaseName); } DataTable dtData = oDBOpsO.GetTableSQL(sSQL.ToString()); TableDB oTable = new TableDB(); List lTbl = new List(); foreach (DataRow orow in dtData.Rows)
Note I have a convention that all views are prefixed with 'vw'
Never underestimate the power of human stupidity RAH
-
Mr Shameel, PLEASE accept my apology...sometimes I write before I think. MANY thanks for the great and simple solution. I hope that you will want to respond to me the next time (and I am sure there will be one)...Best Regards, Pat