Avatar of EricTaylor
EricTaylor

asked on 

C#: adding field name to List of SQL Results

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

C#.NET Programming

Avatar of undefined
Last Comment
EricTaylor
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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

Avatar of EricTaylor
EricTaylor

ASKER

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...
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of EricTaylor
EricTaylor

ASKER

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

ASKER

Thanks
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo