Results 1 to 4 of 4

Thread: Inserting into SQL table only if doesn't already exist?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Question Inserting into SQL table only if doesn't already exist?

    Hi guys,

    (Using .net cf 2.0)
    I have the following insert statement:

    VB Code:
    1. With sqlCmd
    2.             .CommandText = "INSERT INTO photos(visitID, photoType, photoLocation, havePhoto)VALUES(@visitID, @photoType, @photoLocation, @havePhoto)"
    3.             .Parameters.Add(New SqlCeParameter("@visitID", GetCurrentVisit()))
    4.             .Parameters.Add(New SqlCeParameter("@photoLocation", photoLocation))
    5.             .Parameters.Add(New SqlCeParameter("@photoType", photoType))
    6.             .Parameters.Add(New SqlCeParameter("@havePhoto", "Y"))
    7.             .ExecuteNonQuery()
    8.             .Parameters.Clear()
    9.         End With

    But i only want it to do the insert if the value in photoLocation doesn't already exist?
    How can i go about doing this?

    Thanks in advance for any help
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Inserting into SQL table only if doesn't already exist?

    You should set up your database to not allow duplicates in the photoLocation field and then it will not accept duplicates, otherwise you will need to query the database based on the values you are trying to enter to see if they exist.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: Inserting into SQL table only if doesn't already exist?

    Ok thanks for the reply.

    How do i set up the database not to allow duplicates?
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Inserting into SQL table only if doesn't already exist?

    What is the database backend? The basic answer is to create a unquie index on the field.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

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



Click Here to Expand Forum to Full Width