?
Solved

Adding columns to a DataTable in C#

Posted on 2013-05-16
5
Medium Priority
?
316 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
[X]
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
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 2000 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

Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

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…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

719 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