DatabaseMaintenance is a .NET library designed for managing and optimizing database maintenance tasks, including index optimization and script execution. This library provides a flexible interface for configuring and running various maintenance operations with ease.
A special thanks to Ola Hallengren for his exceptional work on SQL Server maintenance solutions. His scripts serve as the foundation for this library, which aims to simplify and enhance database maintenance tasks.
In modern applications, maintaining database performance is crucial. As databases grow and evolve, regular maintenance tasks such as index optimization become necessary to ensure efficient data access and query performance. This library encapsulates common maintenance tasks, providing developers with an easy-to-use interface to manage these processes programmatically.
-
Installation
You can install the
DatabaseMaintenancelibrary via NuGet Package Manager:dotnet add package DatabaseMaintenance
-
Usage
Here’s a basic example of how to use the DatabaseClient and IndexOptions classes to perform index optimization:
using DatabaseMaintenance; var options = new IndexOptions { FragmentationLevel2 = 50, // Customize your options here Databases = "YourDatabaseName" }; var dbClient = new DatabaseClient("YourConnectionString"); // Optional: Create a CancellationTokenSource for canceling long-running tasks var cts = new CancellationTokenSource(); try { await dbClient.ExecuteIndexOptimizeProcedure(options, Console.WriteLine, cts.Token); } catch (OperationCanceledException) { Console.WriteLine("Operation was canceled."); } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); }
-
Customizable Options:
- Set various parameters for index optimization through the
IndexOptionsobject, allowing you to tailor maintenance operations to your specific database needs.
- Set various parameters for index optimization through the
-
Cancellation Tokens:
- Easily manage long-running operations with
CancellationToken. This feature enables you to cancel the execution of maintenance tasks if they exceed your desired time limits, providing flexibility in managing long-running queries.
- Easily manage long-running operations with
-
No Connection Timeouts:
- All connection timeouts have been removed, ensuring that operations can wait indefinitely for completion without unexpected interruptions, allowing for smoother execution of database maintenance tasks.
-
Easy Integration:
- Simple installation via NuGet and straightforward usage patterns make it easy to incorporate into existing .NET applications, enhancing your database management capabilities with minimal effort.
-
Logging Capabilities:
- Output messages and logs during execution can be captured and displayed, facilitating debugging and monitoring of maintenance tasks. For now you need to parse the result yourselves.
The DatabaseMaintenance library provides a robust solution for efficiently managing SQL Server maintenance tasks. By leveraging the well-established work of Ola Hallengren, this library simplifies the complex process of ensuring optimal database performance. With its customizable options, cancellation capabilities, and seamless integration into .NET applications, this library serves as an essential tool for developers looking to maintain their databases effectively and reliably.
This project is licensed under the MIT License. See the LICENSE file for details.