Adding columns to a DataTable in C#

Posted on 2013-05-16
Last Modified: 2013-05-21
I need to create a DataTable where the column definitions are being retrieved from an Oracle Database schema.  I am able to successfully get the information I need but where I run into a problem is setting the DataType on the column.

Here is my code so far:
        public DataTable CreateTable(OracleConnection connectionObj, String owner, String tblname)
            // Creates a DataTable with Table Definition pulled from database schema.
            // Assumptions:  
            //      Works only with Oracle Databases
            //      Database already opened and user has privileges to access schema info
            // Returns:
            //      Returns specified datatable on success, otherwise returns null

            DataTable DT = new DataTable();
            DataColumn column;

            // Check that the database has been opened
            if (connectionObj == null || connectionObj.State != ConnectionState.Open)
                return null;

            // Create Command object
            OracleCommand command = connectionObj.CreateCommand();
            command.Connection = connectionObj;

            // Create Reader
            OracleDataReader reader;
                // Retrieve Table Definition
                command.CommandText =
                    "FROM ALL_TAB_COLUMNS " +
                    "WHERE OWNER=:Owner AND TABLE_NAME=:Tblname";

                // Remove any previously set Parameters
                command.Parameters.Add("Owner", OracleDbType.Varchar2, owner, ParameterDirection.Input);
                command.Parameters.Add("Tblname", OracleDbType.Varchar2, tblname, ParameterDirection.Input);

                reader = command.ExecuteReader();
                while (reader.Read())
                    column = new DataColumn((string)reader["COLUMN_NAME"]);
                    column.DataType = (string)reader["DATA_TYPE"];    <---- This is where I need help
                    column.MaxLength = (Int32)reader["DATA_LENGTH"];
            catch (Exception ex)
                return null;

            return DT;

Open in new window

What I can't figure out is how to set the DataType given the string value in the record returned.  Examples that I have seen use something like the following:  column.DataType = System.Type.GetType("System.Int32");

I was hoping there would be an easy way without having to use a Switch statement looking for all the valid Oracle types.

Any help is greatly appreciated!
Question by:dyarosh
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 39175707

Indeed you can use a helper method that translates the Oracle types to .NET types or you can use a OracleDataAdapter.Fill method to automatically create the columns for you. You can return only one row with nulls or whatever dummy data and delete it before inserting actual data.

Hope this helps,
LVL 42

Expert Comment

ID: 39176774
are you using NET Framework Data Provider for Oracle, or Oracle Data Provider for .NET?

Author Comment

ID: 39181365
I am using Oracle.DataAccess.  

Mishu007 - What is the helper method that you refer to?  I don't want to use the OracleDataAdapter.Fill method because I don't want the data just the schema unless there is a way to get just the schema with the OracleDataAdapter.
LVL 11

Accepted Solution

Mihai Stancescu earned 500 total points
ID: 39183450

You can use the Fill method if you do a select like "select * from <Table> where 1=0", this way the where clause is false and the top row is returned. Unfortunately I can only test this on Sql Server but on Oracle should work the same.


Author Closing Comment

ID: 39185471
Awesome.  Worked like a charm and gave me exactly what I needed.

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question