[thelist] iterate through SQL results without knowing column names

Anthony Baratta anthony at baratta.com
Thu Aug 9 17:30:54 CDT 2007


Here's an example from my code base that opens an SQL Adapter Connection and dumps that to a dataset object. Then with the dataset object you can do your business with the data returned.

        public DataSet execSPForDataSet(string sConnectString, string sProcName, string sDataSetName, Hashtable htSQL)
        {
            SqlConnection dbConn = new SqlConnection();
            DataSet ds = new DataSet();
            SqlDataAdapter sqlAdpt = new SqlDataAdapter();

            try
            {
                dbConn = OpenConnection(sConnectString);
            }
            catch
            {
                return ds;
            }

            SqlCommand cmd = new SqlCommand(sProcName, dbConn);
            cmd.CommandTimeout = cmdTimeout;
            cmd.CommandType = CommandType.StoredProcedure;

            AddSqlParameters(cmd, htSQL);

            try
            {
                sqlAdpt.SelectCommand = cmd;
                sqlAdpt.AcceptChangesDuringFill = false;
                sqlAdpt.Fill(ds, sDataSetName);
            }
            catch (Exception exp)
            {
                dbConn.Close();
                DBError(exp, "execSPForDataSet: sqlAdpt.SelectCommand/Fill (" + sProcName + ")");
                return ds;
            }

            dbConn.Close();
            return ds;
        }

-----Original message-----
From: "Joel D Canfield" joel at streamliine.com
Date: Thu, 09 Aug 2007 15:22:24 -0700
To: thelist at lists.evolt.org
Subject: [thelist] iterate through SQL results without knowing column names

> I need to write the results of a SQL query to a textfile using ASP.NET -
> the problem is that the query, which I don't have access to change, is a
> 'select * from blah' query so I can't iterate through the values and
> write each one into a CSV file.
> 
> Is there a way to iterate through each row, then through each field,
> without knowing what the column names are?
> 
> Or, in fact, what db this is talking to, other than it's probably not
> MSSQL, and might be Access.
> 
> Thanks.
> 
> joel
> -- 
> 
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
> 
> For unsubscribe and other options, including the Tip Harvester 
> and archives of thelist go to: http://lists.evolt.org 
> Workers of the Web, evolt ! 
> 



More information about the thelist mailing list