C# 如何处理Access SQL表达式中字符串变量的单引号或双引号
我用c#写这行代码是为了操作microsoft access数据库C# 如何处理Access SQL表达式中字符串变量的单引号或双引号,c#,ms-access,parameterized-query,C#,Ms Access,Parameterized Query,我用c#写这行代码是为了操作microsoft access数据库 OleDbCommand sComm = new OleDbCommand ("Update TableToBeImport set TextDefault = '" + resxValue + "' WHERE ControlName = '" + resxKey + "'" , c); resxValue和resxKey都是字符串变量 当两个字符串变量很简单时,如“abc”、“xyz”,一切正常,但当一个字符
OleDbCommand sComm = new OleDbCommand
("Update TableToBeImport set TextDefault = '" + resxValue + "'
WHERE ControlName = '" + resxKey + "'" , c);
resxValue
和resxKey
都是字符串变量
当两个字符串变量很简单时,如“abc”、“xyz”,一切正常,但当一个字符串变量变得复杂时,在我的例子中,如下所示:
小贴士:
我知道在c#中,我可以在字符串变量之前使用
@
,因此其中的任何转义字符都将被忽略。但是在ACCESS中,我怎样才能得到相同的结果呢?这对您有用吗
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();
}
}
这对你有用吗
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();
}
}