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

From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 09/30/04


Date: Thu, 30 Sep 2004 11:30:13 -0400

Luminal,

    Instead of trying to figure out the quotes yourself, I would recommend
using a parameterized query, and then just set the value of the parameter to
the value you have. The provider will take care of formatting the values
correctly.

    Hope this helps.

-- 
               - Nicholas Paldino [.NET/C# MVP]
               - mvp@spam.guard.caspershouse.com
"Luminal" <luminal_NOSPAM@hotpop_NOSPAM.com> wrote in message 
news:eKDKJHwpEHA.324@TK2MSFTNGP11.phx.gbl...
> 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: Please help ~ Syntax Error
    ... If this is not for a query, then you will need to change it to use ... The only time the error stops, see below, but then the report doesn't ... So perhaps I should try another approach, maybe my unbound form wasn't ... Syntax error in query expression '(Between ...
    (microsoft.public.access.formscoding)
  • Re: Calculating totals on a Crosstab Query
    ... you are getting closer qrysheds and qryTotal works ... syntax error in query Expression Count(IIf((Is ... FROM QDailyWheplingReport2 ...
    (microsoft.public.access.reports)
  • RE: Problem with queries. Call for help.
    ... Try using a parameterized query instead of inline SQL statement. ... ByVal E As EventArgs) ... Dim objConnection As OleDbConnection ... System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: SQL insert error
    ... I had no problem running the Query in SQL Analyzer. ... > Sub SubmitBtn_Click(ByVal Sender As Object, ByVal E As EventArgs) ... dbParams, Object& executeResult) ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Duplicate records ... sort of
    ... the query has come back with a syntax error ... FROM tblRegions as R INNER JOIN tblContacts as C ... FROM tblRegions INNER JOIN tblContacts ON tblRegions.RegionID = ...
    (microsoft.public.access.queries)