How do I convert a string into safe SQL String?

前端 未结 2 810

I\'m generating some sql insert statements from a bunch of text files.

These text files are generally user input data. I would like to sanitize this data so that it\

相关标签:
2条回答
  • 2020-12-05 14:10

    There is only a single character you have to escape: ansi 0x27, aka the single quote:

    safeString = unsafeString.Replace("'","''");
    
    0 讨论(0)
  • 2020-12-05 14:13

    Don't sanitize your strings. Use parameterized queries instead, as they handle all sanitization.

    You don't specify which database you are using, so I assume it is MS SQL Server. Microsoft has an article on the official ASP.net website about this. Also see MSDN for SqlCommand.Parameters and the AddWithValue method.

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