Solved

Adding columns to a DataTable in C#

Posted on 2013-05-16
5
299 Views
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;
            try
            {
                // Retrieve Table Definition
                command.CommandText =
                    "SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE " +
                    "FROM ALL_TAB_COLUMNS " +
                    "WHERE OWNER=:Owner AND TABLE_NAME=:Tblname";

                // Remove any previously set Parameters
                command.Parameters.Clear();
                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"];
                   DT.Columns.Add(column);
                }
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                reader.Close();
                command.Dispose();
                reader.Dispose();
                column.Dispose();
            }

            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!
0
Comment
Question by:dyarosh
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 39175707
Hi,

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,
Mishu
0
 
LVL 42

Expert Comment

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

Author Comment

by:dyarosh
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.
0
 
LVL 11

Accepted Solution

by:
Mihai Stancescu earned 500 total points
ID: 39183450
Hi,

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.



Regards,
Mishu
0
 

Author Closing Comment

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

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 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