Click here to Skip to main content
12,767,637 members (51,983 online)
Rate this:
 
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 5:38am
Updated 3-Jan-13 6: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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2017
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