Thread: Delete statement with SQL

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    Talking Delete statement with SQL

    Can someone tell me why this function to delete a row in a access database is not working?

    [code]
    public void delete(string table, string file, string cage, string type, string animalName)
    {

    string deleteString;
    deleteString ="delete from `" + table + "\' Where File= " + file + " and CageNo = " + cage + " and AnimalType = `" + type + "\' and Name = `" + animalName+ "\'";

    Console.WriteLine("{0}", deleteString);
    OdbcCommand=new OdbcCommand (deleteString);
    myOdbcCommand.Connection = conn;
    myOdbcCommand.ExecuteNonQuery();
    }
    [\code]







    myOdbcCommand.Connection = conn;
    myOdbcCommand.ExecuteNonQuery();
    Console.WriteLine("{0}", deleteString);
    }

    catch(Exception e)
    {
    Console.WriteLine("failed to delete");
    }
    }
    [

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The table name should not be quoted. The finished SQL statement should look something like this.
    Code:
    DELETE FROM Pets WHERE File='Something.txt' AND CageNo=10 AND AnimalType='Blue Whale' AND Name='Big Boy';
    If you are still having troubles, could you post the completed SQL statement and how it is failing? Is it throwing an exception or just not doing anything?

    JET SQL Reference: DELETE Statement

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    It would have been easier if you a) posted the complete string which you already checked yourself and b) checked what the exception message was, because it will contain an error message.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Also, the end code tag has a forward slash, not a backslash.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can we debug into delete
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2007, 05:45 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. MSVC 2003 Debugging delete
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 09-09-2007, 12:15 PM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. Delete Item
    By emilyh in forum Windows Programming
    Replies: 10
    Last Post: 10-03-2001, 09:33 PM