2013-04-15 76 views
-1

我在將變量加工到現有代碼片段時遇到問題。我確定它很簡單,但我沒有得到任何答案。包含附加變量的C#命令

這是當前代碼,這沒有錯誤:

new SqlCommand(@"xp_cmdshell 'bcp ""SELECT tipReportingSummaryView.payGroupCode, employee.employeeFName + '' '' + employee.employeeLName AS employeeName, tipReportingSummaryView.employeeID, SUM(tipReportingSummaryView.netChargeTips) AS netChargeTips, tipReportingSummaryView.positionCode, rvc.rvcName, ''CHG TIPS'' AS tipType FROM tipReportingSummaryView INNER JOIN employee ON tipReportingSummaryView.employeeID = employee.employeeID INNER JOIN rvc ON tipReportingSummaryView.rvcID = rvc.rvcID WHERE (tipReportingSummaryView.businessDate >= (dateadd(day,(-15),datediff(day,(0),getdate())))) AND (tipReportingSummaryView.businessDate <= (dateadd)) AND (tipReportingSummaryView.payGroupCode = 1) GROUP BY tipReportingSummaryView.payGroupCode, employee.employeeFName + employee.employeeLName, tipReportingSummaryView.employeeID, tipReportingSummaryView.positionCode, rvc.rvcName, employee.employeeFName + '' '' + employee.employeeLName HAVING SUM(tipReportingSummaryView.netChargeTips) <> 0"" queryout " + filePath + " -U sa -P password -c -t ,'", conn)) 

我想要(tipReportingSummaryView.payGroupCode =),以等於payGroup變量的值,所以是這樣的:

new SqlCommand(@"xp_cmdshell 'bcp ""SELECT tipReportingSummaryView.payGroupCode, employee.employeeFName + '' '' + employee.employeeLName AS employeeName, tipReportingSummaryView.employeeID, SUM(tipReportingSummaryView.netChargeTips) AS netChargeTips, tipReportingSummaryView.positionCode, rvc.rvcName, ''CHG TIPS'' AS tipType FROM tipReportingSummaryView INNER JOIN employee ON tipReportingSummaryView.employeeID = employee.employeeID INNER JOIN rvc ON tipReportingSummaryView.rvcID = rvc.rvcID WHERE (tipReportingSummaryView.businessDate >= (dateadd(day,(-15),datediff(day,(0),getdate())))) AND (tipReportingSummaryView.businessDate <= (dateadd)) AND (tipReportingSummaryView.payGroupCode = " + payGroup + ") GROUP BY tipReportingSummaryView.payGroupCode, employee.employeeFName + employee.employeeLName, tipReportingSummaryView.employeeID, tipReportingSummaryView.positionCode, rvc.rvcName, employee.employeeFName + '' '' + employee.employeeLName HAVING SUM(tipReportingSummaryView.netChargeTips) <> 0"" queryout " + filePath + " -U sa -P password -c -t ,'", conn)) 

摘要版

電流:

(tipReportingSummaryView.payGroupCode =1) 

期望:

(tipReportingSummaryView.payGroupCode = " + payGroup + ") 

通過添加「+ payGroup +」,我得到在該行各類語法錯誤,所以它顯然使其彈了出來。

歡迎任何建議。

+0

我覺得payGroup是一個局部變量,他希望它的值在字符串中。 – crashmstr

+1

您可能想要發佈您獲得的特定語法錯誤。 – crashmstr

+0

@crashmstr - 是的,這個數字(刪除以前的評論,以防止任何混淆) – VisualMelon

回答

0

您需要在第二個字符串文字之前放置另一個@否則,雙引號將無法編譯(它會認爲它是兩個文字背對背) - 如下所示。

隔離:" + payGroup + @"

new SqlCommand(@"xp_cmdshell 'bcp ""SELECT tipReportingSummaryView.payGroupCode, employee.employeeFName + '' '' + employee.employeeLName AS employeeName, tipReportingSummaryView.employeeID, SUM(tipReportingSummaryView.netChargeTips) AS netChargeTips, tipReportingSummaryView.positionCode, rvc.rvcName, ''CHG TIPS'' AS tipType FROM tipReportingSummaryView INNER JOIN employee ON tipReportingSummaryView.employeeID = employee.employeeID INNER JOIN rvc ON tipReportingSummaryView.rvcID = rvc.rvcID WHERE (tipReportingSummaryView.businessDate >= (dateadd(day,(-15),datediff(day,(0),getdate())))) AND (tipReportingSummaryView.businessDate <= (dateadd)) AND (tipReportingSummaryView.payGroupCode = " + payGroup + @") GROUP BY tipReportingSummaryView.payGroupCode, employee.employeeFName + employee.employeeLName, tipReportingSummaryView.employeeID, tipReportingSummaryView.positionCode, rvc.rvcName, employee.employeeFName + '' '' + employee.employeeLName HAVING SUM(tipReportingSummaryView.netChargeTips) <> 0"" queryout " + filePath + @" -U sa -P password -c -t ,'", conn)) 

最好放在一個文件路徑串聯後也。

請參閱「逐字字符串」在這裏:MSDN Page On Strings(對不起,找不到一個頁面專門爲他們)

編輯:類似這樣的問題here

+0

謝謝!這就是讓我感到困惑的事情,因爲它不需要與filePath連接有任何不同。 @之前的字符串修復它。 – mkswanson

相關問題