Solved

C#: adding field name to List of SQL Results

Posted on 2014-11-12
5
141 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
  • 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
asp web application 3 43
I need help changing the default value for an @HTML.LabelFor control 4 38
Asp.Net Session Question 2 36
parse string in c# 5 30
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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