mercredi 19 juin 2019

Use Factory Method Design Pattern to execute stored procedures

I have a few stored procedures that I want to execute using factory method pattern from a C# console application.

The following is an example of one of the stored procedures I am trying to execute. The others are similar.

   GO
   SET ANSI_NULLS ON
   GO
   SET QUOTED_IDENTIFIER ON
   GO
   CREATE PROCEDURE [dbo].[OrderHeaders_Delete]
       @OlderThanDays int
   AS
   BEGIN
       DELETE FROM dbo.OrderHeader 
       WHERE LastUpdate < DATEADD(d, @OlderThanDays * -1, GETDATE());
   END

Currently I'm using the following code to do the execution.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace Data_Prune
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection mySqlConnection = new SqlConnection(
                "server=MYSERVER;database=MYDATABASE;uid=UID;pwd=PASSWORD");

            SqlCommand mySqlCmd = mySqlConnection.CreateCommand();
            mySqlCmd.CommandText = "EXECUTE OrderHeaders_Delete @OlderThanDays";
            mySqlCmd.Parameters.Add("@OlderThanDays", SqlDbType.Int).Value = "7";
            mySqlConnection.Open();
            mySqlCmd.ExecuteNonQuery();
            mySqlConnection.Close();                
        }
    }
}

How would I integrate this into a factory method pattern so that I can add on the other stored procedures and have it run through them? The purpose of it is to run everyday as kind of a log deletion job, go through all the stored procedures and delete the appropriate logs.

Aucun commentaire:

Enregistrer un commentaire