1

私はMicrosoft Accessデータベースを操作するためにC#でこの行を書きました

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

resxValue およびresxKey すべて文字列変数です。

2 つの文字列変数が「abc」、「xyz」などの単純な場合は問題ありませんが、1 つの文字列変数が複雑になると、私の場合は次のようになります。

チップ:

  1. 料金プランと RPT エラーを修正するには、「再処理」を使用します

  2. キャリア、トランク グループ、トレーダー エラーを修正するには、「削除して再挿入」を使用します

プログラムはエラーをスローし、デバッグを行うと、sComm.CommandText は次の形式になります。

TableToBeImport set TextDefault = 'TIPS: \r\n1 を更新します。料金プランと RPT エラーを修正するには、「再処理」を使用します\r\n2. キャリア、トランク グループ、トレーダー エラーを修正するには、「削除して再挿入」を使用します WHERE Con​​trolName = 'frmCDRQuery.label7.Text'

私はC#で@、文字列変数の前に使用できることを知っているので、その中のエスケープ文字は無視されます。しかし、アクセスでは、どうすれば同じ結果を得ることができますか?

4

1 に答える 1

1

これはあなたのために働きますか?

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();
    }
}
于 2013-03-10T07:36:58.250 に答える