我想在c#datagridview中顯示動態數據透視的結果。到目前爲止,我已經得到了下面的代碼,但是我很難理解如何在@query字符串中加入@Date變量。我在這裏錯過了什麼?該代碼適用於硬編碼日期,因爲它是返回附加信息:「@Date」附近的語法不正確。請幫幫忙,c#在SQL動態數據透視字符串中添加一個變量
感謝, 一個
da2.SelectCommand = new SqlCommand(@"DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(currency)
FROM Alpha.dbo.Beta
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SET @query = 'SELECT Customer, ' + @cols + ' FROM
(
SELECT
Customer, Amount, Currency
FROM Alpha.dbo.Beta
WHERE Date Between ''2010-01-01'' and '@Date' ----PROBLEM AREA----
) x
PIVOT
(
SUM(Amount)
for Currency in (' + @cols + ')
) AS pvt
ORDER BY Customer; '
execute(@query)", MyConnection);
da2.SelectCommand.Parameters.Add("Date", SqlDbType.DateTime).Value = dateTimePicker4.Text;
ds2.Clear();
da2.Fill(ds2);
怎麼樣在字符串中替換爲文本'「。」「.THEDATE。」...「」? –