escape the ' and " chars to insert them onto an Access database

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Luminal (luminal_NOSPAM_at_hotpop_NOSPAM.com)
Date: 09/30/04


Date: Thu, 30 Sep 2004 16:26:20 +0100

Greetings

I'm having some problems on my C# application. I'm using an access
database and I'm not able to do select queries with the ' character.

My code is this:

// some previous code like open connection

string sqlStatement="SELECT oid FROM formatos WHERE formato='cd\\'s'";

myComm.CommandText=sqlStatement;
OleDbDataReader myDR=myComm.ExecuteReader();

//... and so on

I get the exception message

AccessException: Syntax error (missing operator) in query expression
'formato='cd\'s''.
DeclaringType.FullName:: System.Data.OleDb.OleDbCommand
Method - Void ExecuteCommandTextErrorHandling(Int32)
    at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr)
    at
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS
dbParams, Object& executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object&
executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior
behavior, Object& executeResult)
    at
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior
behavior, String method)
    at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior
behavior)
    at System.Data.OleDb.OleDbCommand.ExecuteReader()
    at
...

AccessError: Syntax error (missing operator) in query expression
'formato='cd\'s''.
Source: Microsoft JET Database Engine
Native Error: -524553244
State: 3075

How can I escape the ' and " chars so I can insert them onto an Access
database?

TIA

Lum



Relevant Pages

  • Re: OleDbException: Could not update; currently locked
    ... Is there any chance that the database is being opened by another ... If you use Access in design mode on a mdb file it will open in exclusive ... dbParams, Object& executeResult) at ... behavior, String method) at ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: SQL insert error
    ... The database is simple access xp database. ... >>Operation must use an updateable query. ... > dbParams, Object& executeResult) ... > sourceControl, String eventArgument) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Befuddled
    ... Check you database, maybe this might have something to do with it. ... have any other tables that reference the table you delete from? ... > Dim Con As OleDbConnection = New OleDbConnection ... > System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) ...
    (microsoft.public.dotnet.languages.vb)
  • Re: It record exists then...
    ... Dim rs As Recordset, db As Database ... Set db = CurrentDb ... Error: Syntax Error. ... If this PartNumber already exists in the InfoTbl (in the field ...
    (microsoft.public.access.formscoding)
  • Re: JET Database errors
    ... I checked the sql of the page and it appears to be correct. ... to edit any records in any databases on my server give the same error). ... a copy of JET Database update 4.0, but I'm not going to install it until I ... > The first thing I would check for is a syntax error in the UPDATE ...
    (microsoft.public.frontpage.programming)