String field with single quotation mark is causing an error when inserting record in table

后端 未结 1 1828
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 01:36

I have below code:

query = \"insert into tblB2B_OrderStatusTopStillInRB (LSRNbr, ShipName, Units, DroppedInRB, EPT, Status, OnTimeStatus, ShipVia, DroppedInR         


        
相关标签:
1条回答
  • 2020-12-02 01:59

    Is there any way to fix that, without removing the single quotation mark from the string?

    Yes - use parameterized SQL instead. You should never use variable values directly in your SQL like this. It can allow SQL injection attacks, cause conversion oddities, and generally make the SQL more confusing to read.

    See the documentation for SqlCommand.Parameters for an example of parameterized SQL.

    Basically, the idea is that your SQL includes references to parameters, e.g.

    INSERT INTO SomeTable(Foo, Bar) VALUES (@Foo, @Bar)
    

    and then you specify the values for @Foo and @Bar separately. The values then aren't part of the SQL itself, so it doesn't matter whether or not they contain characters which would have special meaning within the SQL.

    0 讨论(0)
提交回复
热议问题