从多列检索数据 [英] retrieve data from multiple columns
问题描述
我正在使用VS2008 C#和SQL2008 R2。
我想从多个列中检索数据并合并它们,用逗号分隔它们。
例如我的SQL表UserData中的数据:
No. |用户名|角色1 |角色2 |角色3 |
1 | firoz |用户| READONLY | WRITE |
数据我想在我的GridView GridView1中显示:
No. |用户名|角色|
1 | firoz | USER,READONLY,WRITE |
如何在sql2008中编写SELECT命令,为GrideView编写C#代码
I am using VS2008 C# and SQL2008 R2.
I would like to retrieve data from multiple columns and merge them, separating them by comma.
E.g. Data in my SQL Table UserData:
No. | Username | Role 1 | Role 2 | Role 3 |
1 | firoz| USER | READONLY | WRITE |
Data I would like to display in my GridView GridView1:
No. | Username | Roles |
1 | firoz| USER, READONLY, WRITE |
how i write "SELECT" Command for this in sql2008 and C# code for GrideView
推荐答案
试试这个
select no,username,role1 +','+role2+','+role3 as ROLE from UserData
如果您需要将数值数据类型与varchar数据类型组合,那么您需要使用此
in case you need to combine a numeric data type with varchar datatype then you need to use this
select no,username,cast(no as varchar)+','+role1 +','+role2+','+role3 as ROLE from UserData
这篇关于从多列检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!