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].[I