Click here to Skip to main content
11,641,971 members (64,084 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server MySQL
Hi All,

I have a table(may be DataTable) as

ID Name Department
1  ABC  Oracle
2  DEF  JAVA
3  GHI  C#

Now I want this output should be :

Column1    Column2
ID         1,2,3
Name       ABC,DEF,GHI
Department Oracle,JAVA,C#

Please help me in C# or SQL code.
Thanks in advance.
Posted 3-Jan-13 4:38am
Edited 3-Jan-13 5:34am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
i'm not sure if this is the best approach but you can:

- fill a datatableobject in c# with a
SELECT * FROM myTable
and the DataAdapter. http://msdn.microsoft.com/de-de/library/bh8kx08z%28v=vs.80%29.aspx[^]

- then i would make a list for each column and fill them with the entrys of each row.

- Otherwise, if you want to have a DataTable as result, your can use a doubled foreach loop to handle stuff like this. Or you have a look at:

Transpose a DataTable using C#[^]


hope this helps.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi

You can use from bellow code:

Declare		@ID		nVarChar(500),
		@Name		nVarChar(500),
		@Department	nVarChar(500)
-------------------------------------------------------------------------------
Select	@ID		= N'',
	@Name		= N'',
	@DepartMent	= N''	
------------------------------------------------------------------------------
Select	@ID		= @ID		+ ', ' + Cast(ID As VarChar),
	@Name		= @Name		+ ', ' + Name,
	@Department	= @Department	+ ', ' + Department 
	From	[YourTableName]
-------------------------------------------------------------------------------
Select	@ID		= Substring(@ID, 2, Len(@ID)),
	@Name		= Substring(@Name, 2, Len(@Name)),
	@Department	= Substring(@Department, 2, Len(@Department))
-------------------------------------------------------------------------------
Select	N'ID', @ID
Union
Select	N'Name', @Name
Union
Select	N'Department',@Department


If you want using this action dynamically, please tell me to write dynamic version for you.

I hope it's helpful
  Permalink  
v2

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

  Print Answers RSS
0 DamithSL 268
1 OriginalGriff 150
2 Sergey Alexandrovich Kryukov 134
3 Mika Wendelius 130
4 Afzaal Ahmad Zeeshan 89
0 Mika Wendelius 460
1 OriginalGriff 265
2 DamithSL 258
3 CPallini 169
4 Afzaal Ahmad Zeeshan 149


Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100