Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3310720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:47:32+00:00 2026-05-17T21:47:32+00:00

I am rather new to the more modern ways of coding database persistence, and

  • 0

I am rather new to the more modern ways of coding database persistence, and my intuition is telling me I probably am missing something because my code seems “inelegant” but I do not see another way to do it.

Note: I understand maybe Linq to SQL is considered “dead”. I’m more in a “learning the concepts” mode assuming that I can probably apply this to entity framework or nhibernate later.

Background

I’m getting records from one database and putting them in another. For each record retrieved there are two cases:

  1. It is a brand new record.
  2. It is a previously inserted record that has updated values.

How I Did this in my “old code”

I write a stored procedure called UpsertRecord that checked for the existence of a row and handled both insert and update cases accordingly. This was a pain to type out and part of the reason I started looking at a more modern approach.

USE [dtsynch]
GO

/****** Object:  StoredProcedure [dbo].[InsertStat_test1]    Script Date: 10/27/2010 15:04:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[InsertStat_test1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[InsertStat_test1]
GO

USE [dtsynch]
GO

/****** Object:  StoredProcedure [dbo].[InsertStat_test1]    Script Date: 10/27/2010 15:04:29 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[InsertStat_test1]
 @Uri nvarchar(255),
 @clicks decimal(18,0),
 @clickthru decimal(18, 0),
 @contextualImpressions decimal(18, 0),
 @currency nvarchar(3),
 @epc decimal(18, 2),
 @impressions decimal(18,0),
 @leads decimal(18,0),
 @numSales decimal(18,0),
 @numSubSales decimal(18,0),
 @pid decimal(18,0),
 @revenue decimal(18, 2),
 @saleAmount decimal(18, 2),
 @signups decimal(18, 2),
 @subSaleAmount decimal(18, 2),
 @theyGet decimal(18, 2),
 @weGet decimal(18, 2),
 @Campaign nvarchar(255),
 @Affiliate nvarchar(255),
 @Year decimal(18,0),
 @Month decimal(18,0),
 @Day decimal(18,0)
AS
BEGIN
 SET NOCOUNT ON;
 UPDATE dtsynch.dbo.Stat_test1
 SET clicks = @clicks
       ,clickthru = @clickthru
       ,contextualImpressions = @contextualImpressions
       ,currency = @currency
       ,epc = @epc
       ,impressions = @impressions
       ,leads = @leads
       ,numSales = @numSales
       ,numSubSales = @numSubSales
       ,pid = @pid
       ,revenue = @revenue
       ,saleAmount = @saleAmount
       ,signups = @signups
       ,subSaleAmount = @subSaleAmount
       ,theyGet = @theyGet
       ,weGet = @weGet
       ,Campaign = @Campaign
       ,Affiliate = @Affiliate
       ,[Year] = @Year
       ,[Month] = @Month
       ,[Day] = @Day
 WHERE Uri = @Uri
 IF (@@ROWCOUNT = 0)
 BEGIN
  INSERT INTO dbo.Stat_test1 (
    Uri 
   ,clicks
   ,clickthru
   ,contextualImpressions
   ,currency
   ,epc
   ,impressions
   ,leads
   ,numSales
   ,numSubSales
   ,pid
   ,revenue
   ,saleAmount
   ,signups
   ,subSaleAmount
   ,theyGet
   ,weGet
   ,Campaign
   ,Affiliate
   ,[Year]
   ,[Month]
   ,[Day]
  )
  VALUES (
    @Uri 
   ,@clicks
   ,@clickthru
   ,@contextualImpressions
   ,@currency
   ,@epc
   ,@impressions
   ,@leads
   ,@numSales
   ,@numSubSales
   ,@pid
   ,@revenue
   ,@saleAmount
   ,@signups
   ,@subSaleAmount
   ,@theyGet
   ,@weGet
   ,@Campaign
   ,@Affiliate
   ,@Year
   ,@Month
   ,@Day
  )
 END
END;

GO

What I have so far using Linq to SQL:

List<Stat> stats = service.DirectTrack.GetStatsForCampaign(campaign, dateForRetreivedStats);

//service.SalesForce.UpsertStats(stats);

Syncher db = new Syncher(
 @"Data Source=AARON\SQLEXPRESS;Initial Catalog=syncher;Integrated Security=True",
 XmlMappingSource.FromXml(File.ReadAllText(@"C:\zzz4\projects\svn\repo\1\statsyncher\trunk\src\DAgents.StatSyncher\Generated Code\SqlMetal\Syncher.map")));

foreach (var i in stats)
{
 var cur = (from c in db.DTStatResource
     where c.ApiUrl == i.ExternalId
     select c).FirstOrDefault();

 if (cur == null)
 {
  DTStatResource n = new DTStatResource
  {
   ApiUrl = i.ExternalId,
   Impressions = Convert.ToInt32(i.Impressions),
   ContextualImpressions = Convert.ToInt32(i.ContextualImpressions),
   Clicks = Convert.ToInt32(i.Clicks),
   ClickThru = Convert.ToDecimal(i.ClickThru),
   Leads = Convert.ToInt32(i.Leads),
   Signups = Convert.ToDecimal(i.Signups),
   NumSales = Convert.ToInt32(i.NumSales),
   SaleAmount = Convert.ToDecimal(i.SaleAmount),
   NumSubSales = Convert.ToInt32(i.NumSubSales),
   SubSaleAmount = Convert.ToDecimal(i.SubSaleAmount),
   TheyGet = Convert.ToDecimal(i.TheyGet),
   WeGet = Convert.ToDecimal(i.WeGet),
   EPC = Convert.ToDecimal(i.EPC),
   Revenue = Convert.ToDecimal(i.Revenue),
   Currency = i.Currency,
  };
  db.GetTable<DTStatResource>().InsertOnSubmit(n);
  try
  {
   db.SubmitChanges();
  }
  catch (Exception exxx)
  {
   Console.WriteLine(
    "FAILED-breaking..." + exxx.StackTrace + exxx.Message);
   break;
  }
 }
 else
 {
   cur.ApiUrl = i.ExternalId;
   cur.Impressions = Convert.ToInt32(i.Impressions);
   cur.ContextualImpressions = Convert.ToInt32(i.ContextualImpressions);
   cur.Clicks = Convert.ToInt32(i.Clicks);
   cur.ClickThru = Convert.ToDecimal(i.ClickThru);
   cur.Leads = Convert.ToInt32(i.Leads);
   cur.Signups = Convert.ToDecimal(i.Signups);
   cur.NumSales = Convert.ToInt32(i.NumSales);
   cur.SaleAmount = Convert.ToDecimal(i.SaleAmount);
   cur.NumSubSales = Convert.ToInt32(i.NumSubSales);
   cur.SubSaleAmount = Convert.ToDecimal(i.SubSaleAmount);
   cur.TheyGet = Convert.ToDecimal(i.TheyGet);
   cur.WeGet = Convert.ToDecimal(i.WeGet);
   cur.EPC = Convert.ToDecimal(i.EPC);
   cur.Revenue = Convert.ToDecimal(i.Revenue);
   cur.Currency = i.Currency;
   try
   {
    db.SubmitChanges();
   }
   catch (Exception exxx)
   {
    Console.WriteLine(
     "FAILED-breaking..." + exxx.StackTrace + exxx.Message);
    break;
   }
 }
}

QUESTION: What surprises me is that I have to still type out everything twice. If this is indeed required I’m sure it won’t surprise those more experienced and I’ll be happy to just have a confirmation on that point, otherwise, is there an easier way to get the same functionality (a.k.a Upsert in Salesforce).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:47:33+00:00Added an answer on May 17, 2026 at 9:47 pm
    bool doInsert = (cur == null);
    
    if (doInsert) cur = new DTStatResource();
    
    // Do your member assignments here
    
    if (doInsert)
    {
        db.GetTable<DTStatResource>().InsertOnSubmit(n);
    }
    
    try
    {
        db.SubmitChanges();
    }
    catch (Exception ex)
    {
        // Handle exceptions
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new so I more than likely missing something key. I am using
I'm rather new to MVC and as I'm getting into the whole framework more
I'm rather new to C++ and I'm trying to understand the code over on
I'm rather new to working with multiple threads in a database (most of my
I'm rather new to github social coding and are having trouble following github guidelines.
I am rather new to Sitecore and would like to know a bit more
Rather new to php, so sorry if this seems stupid. I'm really copying a
Being rather new to ASP.MVC I'm looking for a solution to the following routing
I am rather new to the concepts of design patterns and I'm thinking of
I'm rather new to jQuery, so I really don't know if the following problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.