Ritz
Ritz

Reputation: 1253

What's the best way to do an Add/Update DB operation from C#?

I am trying to formulate a skeleton for an Add/update operation in the best possible way(performance wise)?I am thinking of the following way,what would you do?can any suggest if there is a better way?

    public List<DefaultTestPlan> AssignDefaultTestplans(List<DefaultTestPlan> dtp)
    {
        foreach (var dtpdata in dtp)
        {
            if (dtpdata.SoftwareImage != null)
            {
                if (DoesRecordExist(dtp))
                {
                    RemoveDefaultTestPlan(dtp);
                    AddDefaultTestPlan(dtp);
                }
                else
                    AddDefaultTestPlan(dtp);
            }

            else if (dtpdata.Keyword != null )
            {
                if (DoesRecordExist(dtp))
                {
                    RemoveDefaultTestPlan(dtp);
                    AddDefaultTestPlan(dtp);
                }
                else
                    AddDefaultTestPlan(dtp);
            }

            else
            {
                throw new Exception("Invalid  Record given..");
            }

        }

        return dtp;
    }

Upvotes: 0

Views: 38

Answers (1)

James Wood
James Wood

Reputation: 17562

This is too broad to sensibly answer.

The code you have there is going to be fast - it's just a bunch of if statements.

The speed of database access is more likely to have the more significant performance impact.

Also it sounds like you are describing an Upsert function - a feature some databases may already have.

As a side, given you seem to be a little performance concicous have a read of Which is faster?

Upvotes: 1

Related Questions