1

Beginner here experimenting. I have written a small code snippet that enters data from a txt file into a database. As the question states, I want to write an if statement (I think), that checks for duplicates and if there is a duplicate, prevent data entry. Or maybe if the values exist, then donotprocess();, something to that extent.

Im having a little trouble getting started, so if someone could provide some insight or point me into the right direction I would greatly appreciate it.

    public bool UpdateOrderTrackingNumber(string tracking)
{
    trackingNumber = tracking;
    string statement = "INSERT INTO SOP10107 VALUES(@SOPNUMBE, @SOPTYPE, @Tracking_Number);";
    SqlCommand comm = new SqlCommand(statement, connectionPCI);

    comm.Parameters.AddWithValue("SOPNUMBE", orderNumber);
    comm.Parameters.AddWithValue("SOPTYPE", soptype);
    comm.Parameters.AddWithValue("Tracking_Number", tracking);
    try
    {
        comm.Connection.Open();
        comm.ExecuteNonQuery();
        comm.Connection.Close();
    }
    catch (Exception e)
    {
        comm.Connection.Close();
        KaplanFTP.errorMsg = "Database error: " + e.Message;
    }

    return true;
}
Michael J. Barber
  • 24,518
  • 9
  • 68
  • 88
javasocute
  • 648
  • 2
  • 11
  • 28
  • I have a little trouble understanding your question. Could you please clarify when you want to update and when you want to insert?Of course, you can only update items which already exists, and insert items that do not already exist. Is that what you want to do? What i understand from your text is that you dont really want to update, you only want to insert (but ignore/discard duplicates, ie already inserted items). –  Oct 18 '11 at 14:36
  • Right now, my code inserts items into the database whether they exist or not. What I would like to do, is insert items that currently do not exist, and if they do exist, do not update. Hope this helps.. – javasocute Oct 18 '11 at 15:08
  • possible duplicate of [How do I update if exists, insert if not (aka upsert or merge) in MySQL?](http://stackoverflow.com/questions/1218905/how-do-i-update-if-exists-insert-if-not-aka-upsert-or-merge-in-mysql) – onedaywhen Oct 18 '11 at 15:19
  • Take a look at the following question: > http://stackoverflow.com/questions/1218905/how-do-i-update-if-exists-insert-if-not-aka-upsert-or-merge-in-mysql – JP. Oct 18 '11 at 14:28
  • You should mark the question as duplicate in this case, not post an answer with a link to the original – BrokenGlass Oct 18 '11 at 14:31
  • I not too sure if thats what I want to do. Really dont need to update if it exists? – javasocute Oct 18 '11 at 14:33
  • Yep, this is it. Thanks I didnt see that question before. – javasocute Oct 18 '11 at 15:27
  • No, its not. You said you do not want to update - yet that is what it does. It updates if exists. –  Oct 19 '11 at 07:08
  • Urban, I know its not. However its a start in the right direction. – javasocute Oct 19 '11 at 15:45

1 Answers1

1

If you cannot have duplicate rows then you could make those three fields a primary key. Whether you want to depends on the understanding of what that table means.

Is a Sop Number unique always? Or can different Sop Types have Sops with the same number? Is this a join between a Sop and a Tracking, or is Tracking a property of Sop? Can a Sop have many Trackings? What does this table really represent?

If you set up a primary key then the database will prevent your code from inserting duplicates. It's a pretty solid defence, but your code ought to be a bit more friendly and try to prevent it happening rather than failing with a "Duplicate Key" exception.

Edit: Looking at your method name - it seems that your object is a "Sop" and Tracking is a property of it. The method is called "update", so can we assume or require that the Sop already exists? In that case it's just an update statement. How often and in what pattern will you be calling these methods? Will you create Sops and then go around updating their tracking number? Will you always create them complete with tracking number every time?

You may find an object relational mapping framework useful. It's a bigger leap in terms of learning curve, but it can make things a lot easier to program and understand.

Richard Corfield
  • 717
  • 6
  • 19