Escape double quotes in SQL 2005/2008

后端 未结 8 1998
渐次进展
渐次进展 2020-12-21 00:41

I have an international company that has recently been added, which is named \"BLA \"BLAHBLAH\" Ltd. (The double quotes are part of the name. )

Whenever a user tries

相关标签:
8条回答
  • 2020-12-21 01:08

    should be something like

    string sqlCommand = "SELECT c.companyID, c.companyName, c.dateAdded, count(cm.maxID) as NumDirect FROM RussoundGeneral.dbo.Company c LEFT JOIN RussoundGeneral.dbo.CompanyMax cm ON (cm.companyId = c.companyId and cm.maxID is not null ) WHERE CONTAINS ( companyName,  '@strVal' ) group by c.companyID, c.companyName, c.dateAdded ORDER BY c.companyName ASC"
    SqlCommand command = new SqlCommand(strSQLCommand, conn); 
    SqlCommand.Parameters.AddWithValue("@strval", SearchTextBox.Text); 
    0 讨论(0)
  • 2020-12-21 01:09

    I strongly suspect you're building the SQL dynamically - e.g.

    // Bad code, do not use!
    string sql = "SELECT * FROM Foo WHERE X LIKE '" + input + "%'";
    

    That's a really, really bad idea for many reasons - most notably SQL injection attacks. Use parameterised SQL statements instead, where you specify the parameters separately.

    Look at various answers to questions with the sql-injection tag for examples of how to do this properly.

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