dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Escape Ampersand in .net

  1. #1
    Join Date
    Apr 2005
    Posts
    43

    Escape Ampersand in .net

    Hi, I need to run execute a sql select to populate a list in my .net app.
    I am using Oraclecommand as follows:

    System.Data.OracleClient.OracleCommand oCmd = new OracleCommand(sQuery, oracleConn);
    OracleDataReader myReader = oCmd.ExecuteReader()

    However, when the name contains an ampersand, the query does not return any records.

    When I execute following in SQL Plus, it works fine.
    SET SCAN OFF
    select * from table1 where name = 'SI&TAMB'


    Any suggestions how to escape the ampersand in .net code??

  2. #2
    Join Date
    Oct 2010
    Posts
    12

    Re: Escape Ampersand in .net

    You will have to forgive me if this is wrong, but with oledb, which I use often, we simply put a "@" before the declared string. So for example:

    string sql_string = @"SELECT [tbl_users.name] FROM [tbl_users] WHERE [tbl_users.name] LIKE '&'"

    please bare with me if syntax is wrong, im doing this off the top of my head but it should compile fine.

    also you really should think about using PARAMETERS and not directly coding into or referencing the input of objects, but I shall not preach as everyone takes short cuts.

    I hope you find this helpful. If not you can shake your head and declare me a noob


    Regards

  3. #3
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Escape Ampersand in .net

    Have you tried && ?
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)