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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

622 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