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 8213673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:12:00+00:00 2026-06-07T11:12:00+00:00

My SQL code inserts 10,000 records into a table from list. If record already

  • 0

My SQL code inserts 10,000 records into a table from list. If record already exists, it updates a few fields.

Currently it taking more than 10 minutes and timing out unless I restrict the number of records to process. Is there anything in my code which I can do to solve this problem.

   foreach(RMSResponse rmsObj in rmsList) {
        try {
            string connectionString = @"server=localhost\sqlexpress;" + "Trusted_Connection=yes;" + "database=MyDB; " + "connection timeout=30";

            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();

            string str = "";
            str += "SELECT * ";
            str += "FROM RMSResponse ";
            str += "WHERE LeadID = @RecruitID";
            int LeadID;
            Boolean IsPositive = true;

            SqlCommand selectCommand = new SqlCommand(str, conn);
            selectCommand.CommandType = CommandType.Text;
            selectCommand.Parameters.Add(new SqlParameter("@RecruitID", rmsObj.RecruitID));
            SqlDataReader sqlDataReader = selectCommand.ExecuteReader();
            bool hasRows = sqlDataReader.HasRows;

            while (sqlDataReader.Read()) {
                LeadID = sqlDataReader.GetInt32(1);
                IsPositive = sqlDataReader.GetBoolean(2);
                IsPositive = (IsPositive == true) ? false : true;
                Console.WriteLine("Lead ID: " + LeadID + " IsPositive: " + IsPositive);
            }

            sqlDataReader.Close();

            if (hasRows) {
                SqlCommand updateCommand = new SqlCommand("UPDATE RMSResponse set IsPositive=@IsPositive, OptOutDate=@OptOutDate where LeadID=@LeadID", conn);
                updateCommand.Parameters.AddWithValue("@LeadID", rmsObj.RecruitID);
                updateCommand.Parameters.AddWithValue("@IsPositive", IsPositive);
                updateCommand.Parameters.AddWithValue("@OptOutDate", DateTime.Now);
                updateCommand.ExecuteNonQuery();
                sqlDataReader.Close();
            }

            if (!hasRows) {
                SqlCommand insertCommand = new SqlCommand("INSERT INTO RMSResponse (LeadID, IsPositive, ReceivedDate) " + "VALUES(@LeadID, @IsPositive, @ReceivedDate)", conn);
                insertCommand.Parameters.AddWithValue("@LeadID", rmsObj.RecruitID);
                insertCommand.Parameters.AddWithValue("@IsPositive", true);
                insertCommand.Parameters.AddWithValue("@ReceivedDate", DateTime.Now);
                int rows = insertCommand.ExecuteNonQuery();
            }
        } catch (SqlException ex) {
            Console.WriteLine(ex.Message);
        }
    }
  • 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-06-07T11:12:02+00:00Added an answer on June 7, 2026 at 11:12 am

    You can move the update to SQL – all you are doing is setting the OptOutDate to be today. You can pass in the list of lead IDs to a batch update statement.

    To insert records, you could bulk insert into a staging table, then execute SQL to insert the data for IDs that aren’t already in the table.

    There isn’t much logic in your C#, so pulling the data out then putting it back in is making it unnecessarily slow.


    If you don’t want to go down this root, then other tips include:

    • Open one connection outside of the loop
    • Create one SqlCommand object outside of the loop and reuse it by resetting the paramenters
    • Change your select SQL to only select the columns you need, not *
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a subroutine to insert a record to sql server table. The code:
I have the following code: def add_record(self,values): self.sql.execute(INSERT INTO TEST VALUES (?,?) % values)
i have some SQL code that is inserting values from another (non sql-based) system.
I have a mysql table like this (sql): CREATE TABLE IF NOT EXISTS silver_and_pgm
Recently, I write a python code to insert HTML text into table. After my
I am importing approx 350,000 lines from a CSV file into MySQL using PHP
I'm developing a program in C# which inserts around a 100,000 rows into a
I have the following table: tbl_ProductCatg Id IDENTITY Code Description a few more. Id
Question How can I alter the SQL code below to ensure that the inserts
Given the SQL code below: create table A(a integer, b integer, c double); insert

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.