Click here to Skip to main content
Licence CPOL
First Posted 23 Oct 2008
Views 14,500
Bookmarked 24 times

SQL Pivot and Table Adapter

By | 23 Oct 2008 | Article
Using TableAdapter to display Pivot result without code.
 
Part of The SQL Zone sponsored by
See Also

Introduction

The Pivot operator in SQL Server 2005 is very useful for displaying a crosstab report. Using the DataGrid control, we can easily display a crosstab report in a Windows form. This article shows you how to do it without writing any code.

Procedures

First, we need to create a table to manipulate and show the data. Using SQL2005 Server Management Studio, you should be able to crate a table as in Table 1. The table name I used was Pivt. You are free to use any name.

Next, you can try out the Pivot SQL as in listing 1 in the SQL2005 query window. This serves as a check that the statement is correct and the Pivot SQL produces the desired result.

Table1.PNG

Listing_1.PNG

Next, in Visual Studio 2005, open a new Windows project and create a “DataTableadapter”. Make all the necessary connections to the database server. While in the process of creating the “DataTableAdapter”, paste the SQL Pivot statement in the query window. Continue with all the steps until the “DataTableAdapter” is created. In the “DataTableAdapter”, add in all the new columns and select the respective data source one at a time, as shown in Figure 1.

Figure_1.PNG

Finally, drag and drop the dataset from the data source on to the form, and execute the form once you have finished. A cross tab report similar to Figure 2 will appear.

Figure_2.PNG

Points of Interest

A simple Pivot table has been created without writing any C# or VB code. The aggregate function used in the Pivot statement was “SUM”. If you need to check the total count you could change it to “COUNT”. It should be pointed out that in normal circumstances, the query window in Visual Studio will not support a Pivot statement. However, in this article, we show that it can be done.

History

None.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

TTGOH



New Zealand New Zealand

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSome good things are hard to find Pinmemberheck-20:41 28 Oct '08  
GeneralRe: Some good things are hard to find PinmemberTTGOH11:48 29 Oct '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 23 Oct 2008
Article Copyright 2008 by TTGOH
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid