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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:10:22+00:00 2026-05-27T20:10:22+00:00

I am writing into two tables on SQL server row by row from C#.

  • 0

I am writing into two tables on SQL server row by row from C#.

My C# app is passing parameters into 2 stored procedures which are each inserting rows into tables.

Each time I call a stored procedure I open and then close the connection.

I need to write about 100m rows into the database.

Should I be closing and opening the connection every time I call the stored procedure?

Here is an example what I am doing:

public static void Insert_TestResults(TestResults testresults)
        {
            try
            {
                DbConnection cn = GetConnection2();
                cn.Open();

                // stored procedure
                DbCommand cmd = GetStoredProcCommand(cn, "Insert_TestResults");
                DbParameter param;

                param = CreateInParameter("TestName", DbType.String);
                param.Value = testresults.TestName;
                cmd.Parameters.Add(param);


                if (testresults.Result != -9999999999M)
                {
                    param = CreateInParameter("Result", DbType.Decimal);
                    param.Value = testresults.Result;
                    cmd.Parameters.Add(param);
                }


                param = CreateInParameter("NonNumericResult", DbType.String);
                param.Value = testresults.NonNumericResult;
                cmd.Parameters.Add(param);

                param = CreateInParameter("QuickLabDumpID", DbType.Int32);
                param.Value = testresults.QuickLabDumpID;
                cmd.Parameters.Add(param);
                // execute
                cmd.ExecuteNonQuery();

                if (cn.State == ConnectionState.Open)
                    cn.Close();

            }
            catch (Exception e)
            {

                throw e;
            }

        }

Here is the stored procedure on the server:

USE [SalesDWH]
GO
/****** Object:  StoredProcedure [dbo].[Insert_TestResults]    Script Date: 12/26/2011 10:45:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[Insert_TestResults]
    -- Add the parameters for the stored procedure here

    @TestName varchar (500),
    @Result decimal (18,4)=null,
    @NonNumericResult varchar (50)=null, 
    @QuickLabDumpid int

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

INSERT INTO [SalesDWH].[dbo].[TestResults]
           ([TestName]
           ,[Result]
           ,nonnumericresult
           ,[QuickLabDumpid])
     VALUES
           (@TestName,@Result,@nonnumericresult,@QuickLabDumpID)


END

For about 100m rows it will take like 3 days. This seems too slow for me. What can I do to speed this up? What are the standards on opening/closing connection so many times?

  • 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-27T20:10:23+00:00Added an answer on May 27, 2026 at 8:10 pm

    If you’re on SQL Server 2008 you could send multiple records at once through a table-valued parameter:

    create type testResultUpload as table
    (
        TestName varchar(500),
        Result decimal(18,4) null,
        NonNumericResult varchar(50) null, 
        QuickLabDumpid int
    )
    

    Then you could build up a DataTable on the client side and pass it to sql as one chunk. Though, you may want to do a thousand at a time to start off with.

    You’d have to ammend your stored procedure to deal with an input record set, starting with the parameter definition

    alter proc Insert_TestResult
    (
        @testResultUpload testResultUpload readonly -- tvp must be readonly
    )
    as begin       
    
        -- This is short and sweet for demonstrative purposes
        -- but you should explicitly list your columns
        insert [SalesDWH].[dbo].[TestResults] 
        select
         *
        from @testResultImport
    
    end
    

    Then on your client side:

    // create your datatable in the form of the newly created sql type
    var dt = new DataTable();
    dt.Columns.Add("TestName", typeof(String));
    dt.Columns.Add("Result", typeof(Decimal));
    dt.Columns.Add("NonNumericResult", typeof(String));
    dt.Columns.Add("QuickLabDumpid", typeof(String));
    
    // add your rows here (maybe do it in steps of a thousand
    // 100 Million over the pipe at once is ill-advised)
    // call the following code to hit sql
    
    using (var cnx = new SqlConnection("your connection string"))
    using (var cmd = new SqlCommand {
        Connection = cnx,
        CommandType = CommandType.StoredProcedure,
        CommandText = "dbo.Insert_TestResults",
        Parameters = {
            new SqlParameter {
                ParameterName = "@testResultUpload",
                Value = dt,
                SqlDbType = SqlDbType.Structured // make sure to specify structured
            }
        }
    })
    {
        cnx.Open();
        cmd.ExecuteNonQuery();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing tsql for SQL Server 2008. I've got two tables with roughly 2
I have a web application that loads some 50 tables from SQL Server into
When you are writing a linq query that involves two tables/entities for which you
I'm writing into a session cookie from my plugin in chrome. The browser seems
I'm looking into writing a simple synchronization ability into my app and one of
We have an application which uses an instance of Sql Server locally for its
I have an oracle DB where a program is writing into two columns when
There is a requirement to load a list of items(ArrayList) from two different tables.
I have two massive tables with about 100 million records each and I'm afraid
I'm writing an application that will have a SQL Server backend that will store

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.