Solved

C#: adding field name to List of SQL Results

Posted on 2014-11-12
5
133 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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now