-1

I have a stored procedure which purges data from a table, and I have multiple stored procedures. I want to call this stored procedure from a C# batch process. Which logic do I need to implement to call that stored procedure either sequentially or multithreaded, because I want to pass command line argument to purge multiple tables.

For example: command line argument could be like (table1, table2, table3)

Then the stored procedure will run and start purging. What technique can I choose either sequentially purging or (multi)thread wise purging (I have separate method for each table to call).

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104

1 Answers1

3

Not enough information to go on but here are two examples.

I am assuming you're using SQL Server.

Threading:

using System.Data.SqlClient;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        string[] tables = args; // Get table names from command line arguments

        string connectionString = "your_connection_string";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            foreach (string table in tables)
            {
                Thread thread = new Thread(() => PurgeDataFromTable(connection, table));
                thread.Start();
            }
        }
    }

    static void PurgeDataFromTable(SqlConnection connection, string tableName)
    {
        using (SqlCommand command = new SqlCommand("your_stored_procedure_name_here", connection))
        {
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@tableName", tableName);
            command.ExecuteNonQuery();
        }
    }
}

Sequentially:

using System.Data.SqlClient;

class Program
{
    static void Main(string[] args)
    {
        string[] tables = args; // Get table names from command line arguments

        string connectionString = "your_connection_string";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            foreach (string table in tables)
            {
                PurgeDataFromTable(connection, table);
            }
        }
    }

    static void PurgeDataFromTable(SqlConnection connection, string tableName)
    {
        using (SqlCommand command = new SqlCommand("your_stored_procedure_name", connection))
        {
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@tableName", tableName);
            command.ExecuteNonQuery();
        }
    }
}

Also take a look here: Asynchronous in C#

Edit: added comments in code.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Christophermp
  • 176
  • 1
  • 3
  • 13