我的格式如下:
ID NARR3
1 One
2 Two
1 One
2 Four
2 Five
我需要这样的输出:
ID NARR3
1 One
2 Five,Four,Two
我尝试了以下SQL服务器查询,它运行正常。
select
t1.[ID],
STUFF((select ',' + st.[NARR3]
from (select Distinct(t.[NARR3]) from [IMMS$] t
where t.[ID] = t1.[ID]) st
for xml path
)1,1,'') as [NARR3]
from [IMMS] t1
group by [ID]
但我希望这可以与OLEDB连接一起使用。当我尝试使用OLEDB连接运行时,出现以下错误:
Syntax error in query expression 'STUFF((select ',' + st.[NARR3] from (select Distinct(t.[NARR3]) from [IMMS] t where t.[ID] = t1.[ID]) st for xml path),1,1,'')'.
我使用的dotnet代码如下:
DataTable dt = new DataTable();
using (Command com = new Command(connection, query, st, 0))
{
dt = com.PopulateDataTable(dt);
dt.TableName = tablename;
}
return dt;
where
connection = Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\IMMS SHEET FILLED WITH DETAILS1.xlsx;Extended Properties='Excel 12.0 XML;IMEX=1;HDR=YES';
tableName = Table1
query = Above query
st = OLEDB
PopulateDataTable函数如下:
public DataTable PopulateDataTable(DataTable dt)
{
_connection.Open();
using (_adapter)
{
_adapter.Fill(dt);enter code here
}
_connection.Close();//conn
return dt;
}
帮助将非常值得注意。我不知道OLEDB查询中缺少什么。