using System.Data.SqlClient;
static string connectionString = "you connection string";
static int totalThreads = 10;
static int recordsPerThread = 100;
static object obj = new object();
static void Main(string[] args)
Thread[] threads = new Thread[totalThreads];
for (int i = 0; i < totalThreads; i++)
threads[i] = new Thread(InsertData);
foreach (Thread thread in threads)
Console.WriteLine("Data insertion completed. Press any key to exit.");
static void InsertData(object threadNumber)
int threadId = (int)threadNumber;
int startId = (threadId - 1) * recordsPerThread + 1;
int endId = threadId * recordsPerThread;
Console.WriteLine($"Thread {threadId} Started...");
using (SqlConnection connection = new SqlConnection(connectionString))
for (int i = startId; i <= endId; i++)
using (SqlCommand command = connection.CreateCommand())
command.CommandText = "INSERT INTO imas.PowderEvent (PowderID, Text, InternalText, Value) VALUES (@PowderId, @Text, '', @Value); SELECT SCOPE_IDENTITY();";
command.Parameters.AddWithValue("@PowderId", 13000);
command.Parameters.AddWithValue("@Text", $"Data from Thread {threadId}");
command.Parameters.AddWithValue("@Value", i * 10.0);
int insertedId = Convert.ToInt32(command.ExecuteScalar());
Console.WriteLine($"Thread :{threadId} Insert in ID: {insertedId} ");
Console.WriteLine($"Thread {threadId} completed.");
Console.WriteLine($"Thread {threadId} Error :{e} ");