Ad Code

Responsive Advertisement

.Net Task 01: Delete all records when time is 12:35 AM

.Net Task 01: Delete all records when time is 12:35 AM.

To delete all records from a database table at a specific time (12:35 AM), you can create a scheduled task using a console application in .NET and the Windows Task Scheduler. Here's a step-by-step guide on how to do this:

1. Create a Console Application

Create a new console application in Visual Studio and add the necessary references to `System.Data.SqlClient` and `System.Configuration`.

2. Set Up Configuration

Ensure your `app.config` file has the correct connection string:

```xml
<configuration>
  <connectionStrings>
    <add name="BajajTMSResultMasterEntities"
         connectionString="data source=DC-SOF-001\SQLSERVER2014;initial catalog=TRAFFIC_MANAGEMENT_CDMS_RESULT_MASTER_BAJAJ;user id=sa;password=dmc@123;MultipleActiveResultSets=True;App=EntityFramework"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>
```

3. Write the Code to Delete Records

Here’s the code for the console application that connects to the database and deletes all records from the specified table:

```csharp
using System;
using System.Data.SqlClient;
using System.Configuration;

namespace ScheduledDatabaseCleaner
{
    class Program
    {
        static void Main(string[] args)
        {
            DeleteAllRecords();
        }

        private static void DeleteAllRecords()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["BajajTMSResultMasterEntities"].ConnectionString;
            string deleteQuery = "DELETE FROM YourTableName"; // Replace 'YourTableName' with the actual table name.

            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand(deleteQuery, connection))
                    {
                        int rowsAffected = command.ExecuteNonQuery();
                        Console.WriteLine($"{rowsAffected} rows deleted successfully.");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: " + ex.Message);
            }
        }
    }
}
```

4. Compile the Console Application

Build your console application to create an executable (`.exe`) file.

5. Schedule the Task

Now, you can schedule this console application to run at 12:35 AM using the Windows Task Scheduler.

Steps to Schedule the Task:

1. Open Task Scheduler:
   - Go to the Start menu, search for "Task Scheduler," and open it.

2. Create a New Task:
   - Click on "Create Task" on the right pane of the Task Scheduler.

3. General Tab:
   - Enter a name for your task, like "Delete Database Records."

4. Triggers Tab:
   - Click on "New..."
   - Set the start time to 12:35 AM.
   - Set the frequency to "Daily."
   - Click "OK."

5. Actions Tab:
   - Click on "New..."
   - In the "Action" dropdown, select "Start a program."
   - In the "Program/script" box, click "Browse" and select the compiled `.exe` file of your console application.
   - Click "OK."

6. Conditions and Settings (Optional):
   - Adjust any conditions or settings according to your needs, such as waking the computer to run the task.

7. Save the Task:
   - Click "OK" to save the task.

6. Testing the Scheduled Task

You can test the task by manually running it in Task Scheduler to ensure it works correctly. Right-click on the task and select "Run."

Summary

By following these steps, you’ve set up a console application to delete all records from a database table and scheduled it to run at 12:35 AM using the Windows Task Scheduler. This setup ensures that your database maintenance tasks are automated and run without manual intervention.

Post a Comment

0 Comments

Ad Code

Responsive Advertisement