?
Solved

C#: adding field name to List of SQL Results

Posted on 2014-11-12
5
Medium Priority
?
149 Views
Last Modified: 2014-11-20
I've written a piece to execute a SQL query and put the results into a List<object> where each record contains the fields of data from each SQL record returned. However, in my results, I no longer have the SQL field names that I can see but only have a dozen numbered fields. Is there a relatively easy way to keep the field names with the corresponding data? What I'd like to be able to do when dealing with the list is to interact with "JobID" rather than merely column 1, etc. But I've tried to write this in a generic way because this method will have to deal with many different SQL queries of varying complexity... returning anything from a single record to thousands (mostly in the 0 to several hundred range).

If you answer, please be very basic. While I've been programming for a while (almost all Delphi), I've only been working with C# for less than a week. I'm still very much in the "pirate" stage... looking for various answers online and "borrowing" and modifying to fit what I need to do. So while what I've written here works, it's is beyond my comfort level at this point. If you answer assuming that I can just pop off what's here, your answer will be over my head. (Generic suggestions to make this better or more efficient are also welcome.)

Anyway, here's what I've got so far.
    public class QueryResults : List<object[]>
    {
        public void GetData(string SQL, params object[] RequestParams)
        {
            using (FbConnection ibCon = new FbConnection(DBConnection.CreateConnString()))
            {
                try
                {
                    FbCommand cmdFBQuery = new FbCommand();

                    cmdFBQuery.Connection = ibCon;
                    cmdFBQuery.CommandType = CommandType.Text;
                    cmdFBQuery.CommandText = SQL;
                    int i = 0;
                    foreach (object obj in RequestParams)
                    {
                        cmdFBQuery.Parameters.Add(new FbParameter(i++.ToString(), obj));
                    }

                    ibCon.Open();
                    using (var reader = cmdFBQuery.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var columns = new object[reader.FieldCount];
                            reader.GetValues(columns);
                            this.Add(columns);
                        }
                    }
                }
                catch (Exception ex)
                {
                    // handle error here
                }
            }
        }
    }

Open in new window

0
Comment
Question by:EricTaylor
[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
  • 3
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40439277
you need to add the field names before doing the loop on the data.
however, as there is no GetColumnNames() function like the GetValues, you have to loop on the columns.
something like this (untested, not on a PC with .net installed...)

using (var reader = cmdFBQuery.ExecuteReader())
                    {
                       var columns = new object[reader.FieldCount];
                        int i = 0;
                       foreach( DBColumn f in reader.Columns)
                      {
                              columns[i++] = f.Name;
                      }
     
                        while (reader.Read())
                        {
                            var columns = new object[reader.FieldCount];
                            reader.GetValues(columns);
                            this.Add(columns);
                        }
                    }

Open in new window

0
 

Author Comment

by:EricTaylor
ID: 40443373
hmmm... this seems to have difficulties: compiler is objecting to reuse of columns and also saying that reader.Columns is invalid.

Oops... thought I responded yesterday, but this is still sitting here...
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40445673
sorry, it's not Columns, but Fields...

for ( System.Int32 iCol = 0; iCol < reader.FieldCount; iCol ++ )
  {
     columns[iCol] =reader.GetName( iCol ).ToString();
 }
0
 

Author Comment

by:EricTaylor
ID: 40451323
sorry for delay in interacting with your solution, Guy: got interrupted with another project. Will be back on this in a day or so.
0
 

Author Closing Comment

by:EricTaylor
ID: 40456454
Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

764 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