Jeff's SQL Server Blog

Random Thoughts & Cartesian Products with Microsoft SQL Server
posts - 157, comments - 2686, trackbacks - 64

My Links

Advertisement

News

Welcome to my weblog. My name is Jeff Smith, I am software developer in Boston, MA and I was recently named a 2009 SQL Server MVP. Check in frequently for tips, tricks, commentary and ideas on SQL Server and .NET programming.


Subscribe





Archives

Post Categories

Programming

Sports

SQL

Pivot Sample (C#)

private void Sample()

        {
            // call this from a form ....

            SqlConnection conn;

            SqlCommand com;

 

            DataGrid dg = new DataGrid();

            dg.Parent = this;

            dg.Dock = DockStyle.Fill;

 

            String SQL =

                "select o.customerID, c.CompanyName, p.productName, sum(od.quantity) as Qty " +

                " from orders o " +

                " inner join [order details] od on o.orderID = od.orderID " +

                " inner join Products p on od.ProductID = p.ProductID " +

                " inner join Customers c  on o.CustomerID = c.CustomerID " +

                " group by o.customerID, c.CompanyName, p.ProductName " +

                " order by o.customerID";

 

            conn = new SqlConnection( "Server=(local);Database=Northwind;uid=xx;pwd=xx");

            conn.Open();

            com = new SqlCommand(SQL, conn);

            try

            {

                dg.DataSource = Pivot(com.ExecuteReader(),"CustomerID","ProductName","Qty");

            }

            catch (SqlException ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

Print | posted on Wednesday, May 11, 2005 3:10 PM |

Feedback

Gravatar

# Cross Tabs at the Presentation Layer

5/11/2005 4:26 PM | Welcome to Jeff's Blog
Gravatar

# re: Pivot Sample (C#)

This does not work, do you need to have a library included?
5/25/2005 5:24 PM | Drew
Gravatar

# re: Pivot Sample (C#)

My Bad, didn't see function link.
5/25/2005 5:26 PM | Drew Again
Gravatar

# HINT: The word "Pivot" in the sample above is a link to the code!!

brilliant example!
i had the same experience as Drew at first --> i couldn't find the link to the "Pivot" function
8/2/2005 10:58 PM | leon
Gravatar

# re: Pivot Sample (C#)

hello
please send me this sample with VB language
bye
10/9/2005 7:07 AM | koorosh zahedi
Gravatar

# re: Pivot Sample (C#)

"hello
please send me this sample with VB language
bye
"

Ugh... can you do ANY work for yourself?
7/21/2006 4:07 PM | thankfullprogrammer
Gravatar

# re: Pivot Sample (C#)


This is great - worked first time.

I'm especuially impressed that extra PK columns are also included in the results set by default
9/5/2006 6:41 AM | srous
Gravatar

# re: Pivot Sample (C#)

random pivot rules
7/8/2008 10:04 AM | eric turner
Gravatar

# re: Pivot Sample (C#)

Great job. thanks for sharing.
11/6/2009 4:44 AM | oalp
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET