如何处理Access SQL表达式中字符串变量中的单引号或双引号

本文关键字:变量 字符串 单引号 表达式 何处理 处理 SQL Access | 更新日期: 2023-09-27 18:03:22

这一行是我用c#写的,用来操作microsoft access数据库

OleDbCommand sComm = new OleDbCommand
  ("Update TableToBeImport set TextDefault = '" + resxValue + "' 
    WHERE ControlName = '" + resxKey + "'" , c);

resxValueresxKey都是字符串变量。

当两个字符串变量是简单的,如"abc","xyz",事情是OK的,但当一个字符串变量变得复杂,在我的情况下,像这样:

提示:

  1. 确定费率计划&报告错误使用'reprocess'

  2. 修复运营商,中继组,贸易商错误,使用'delete and reinsert'

程序抛出一个错误,当我执行调试时,sComm。CommandText是这样的形式:

Update TableToBeImport set TextDefault = 'TIPS: 'r'n1。固定费率计划,RPT错误使用'reprocess''r'n2。修复运营商、干线集团、贸易商的错误;WHERE ControlName = 'frmCDRQuery.label7.Text'

我知道在c#中,我可以在字符串变量之前使用@,因此其中的任何转义字符将被忽略。但是在access中,我怎样才能得到相同的结果呢?

如何处理Access SQL表达式中字符串变量中的单引号或双引号

这对你有用吗?

var resxValue = ""; // obviously this will be whatever the type and value that is required
var resxKey = ""; // obviously this will be whatever the type and value that is required
// I am assuming here that you are already programming with a `using` statement
using (OleDbConnection c = new OleDbConnection(""))
{
    using (OleDbCommand sComm = new OleDbCommand("UPDATE TableToBeImport SET TextDefault = @p0 WHERE ControlName = @p1", c))
    {
        sComm.Parameters.Add(new OleDbParameter("@p0", resxValue));
        sComm.Parameters.Add(new OleDbParameter("@p1", resxKey));
        // rest of your code here E.G., sComm.ExecuteNonQuery();
    }
}