Title: rows and columes looping                                 If you need copy same format use firefox or chrome or safari but iexplorer not work


Preview of Code
select * from DailyIncome                                 -- Colums to pivot
pivot (
   max (IncomeAmount)                                                    -- Pivot on this column
   for IncomeDay in ([MON],[TUE],[WED],[THU],[FRI],[SAT],[SUN]))         -- Make colum where IncomeDay is in one of these.
   as MaxIncomePerDay                                                     -- Pivot table alias
where VendorId in (`SPIKE`)                               -- Select only for this 
 
 
        static void Main(string[] args)
        {
            string cs = @"Data Source=<your server>;Initial Catalog=<your database>;Integrated Security=SSPI";
            try
            {
                using (SqlConnection con = new SqlConnection(cs))
                {
                    con.Open();
 
                    string sql = "select * from DailyIncome pivot (avg (IncomeAmount) for IncomeDay in ([MON],[TUE],[WED],[THU],[FRI],[SAT],[SUN])) as AvgIncomePerDay";
                    SqlDataAdapter da = new SqlDataAdapter(sql, con);
                    DataTable dt = new DataTable("AverageIncomeForVendor");
                    da.Fill(dt);
 
                    // Bind the DataTable to whatever, just displaying it in console here.
 
                    int colCount = dt.Columns.Count;
                    foreach (DataRow row in dt.Rows)
                    {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < colCount; i++)
                        {
                            sb.Append(row[i].ToString() + "\t");
                        }
                        Console.WriteLine(sb.ToString());
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }