具有保留字的c#中的SQL更新语句

时间:2018-07-18 15:23:00

标签: c# sql sql-update reserved-words

我对更新语句有疑问

在列名中,我有如下字符:%,[]。在“插入到”语句中,我只是将其放在引号中并起作用。但是无法执行此更新语句:

UPDATE WIG_BANKI 
SET "Nazwa_spolki" = @BankNames, 
"Pakiet" = @Packet, 
"Udzial_w_portfelu[%]" = @ShareInPortfolio, 
"Udzial_w_obrocie[%]" = @ShareOfTurnover, 
"Wplyw_na_zmiane_indeksu[%]" = @ImpactOnChangeOfIndex, 
"Zmiana_kursu_spolki[%]" = @ExchangeRate, 
"Kurs[PLN]" = @PLN_ExchangeRate 
 WHERE "Nazwa_spolki" != @BankNames, 
       "Pakiet" != @Packet, 
       "Udzial_w_portfelu[%]" != @ShareInPortfolio, 
       "Udzial_w_obrocie[%]" != @ShareOfTurnover, 
       "Wplyw_na_zmiane_indeksu[%]" != @ImpactOnChangeOfIndex, 
       "Zmiana_kursu_spolki[%]" != @ExchangeRate, 
       "Kurs[PLN]" != @PLN_ExchangeRate

然后我像这样执行整个语句:

command.Parameters[Globals.columnsNamesArrayDB[i]].Value = array[i].ToString(); // here in for loop I set what is @BankNames etc.
command.CommandText = sbAllDataUpdate.ToString();
command.ExecuteNonQuery();

然后显示此消息错误

System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near ','.

我不知道如何纠正

1 个答案:

答案 0 :(得分:3)

对于WHERE,您可以使用逻辑运算符AND OR分隔条件

 WHERE "Nazwa_spolki" != @BankNames AND
       "Pakiet" != @Packet AND
       "Udzial_w_portfelu[%]" != @ShareInPortfolio AND
       "Udzial_w_obrocie[%]" != @ShareOfTurnover AND
       "Wplyw_na_zmiane_indeksu[%]" != @ImpactOnChangeOfIndex AND 
       "Zmiana_kursu_spolki[%]" != @ExchangeRate AND 
       "Kurs[PLN]" != @PLN_ExchangeRate