Table of Contents

SqlDataManager Class

Definition

Namespace
Cuemon.Data.SqlClient
Assemblies
Cuemon.Data.SqlClient.dll
Source
src/Cuemon.Data.SqlClient/SqlDataManager.cs

The SqlDataManager is the primary class of the Cuemon.Data.SqlClient namespace that can be used to execute commands targeted Microsoft SQL Server.

public class SqlDataManager : DataManager, IConfigurable<DataManagerOptions>
Inheritance
SqlDataManager
Implements
Inherited Members

Examples

The following example demonstrates how to use SqlDataManager to execute commands against Microsoft SQL Server.

using System;
using Cuemon.Collections.Generic;
using Cuemon.Data;
using Cuemon.Data.SqlClient;
using Microsoft.Data.SqlClient;

namespace MyApp.Examples;

public class Example
{
    public void Run()
    {

        // Configure the SQL data manager with a connection string
        var manager = new SqlDataManager(o =>
        {
            o.ConnectionString = "Server=.;Database=AdventureWorks;Trusted_Connection=True;TrustServerCertificate=True;";
        });

        // Execute a scalar query (implicit string to DataStatement conversion)
        var productCount = manager.ExecuteScalar("SELECT COUNT(*) FROM Production.Product");
        Console.WriteLine($"Product count: {productCount}");

        // Execute a reader command with parameters
using var reader = manager.ExecuteReader(new DataStatement(
            "SELECT ProductID, Name, ListPrice FROM Production.Product WHERE ListPrice > @minPrice",
            o => o.Parameters = Arguments.ToArrayOf(new SqlParameter("@minPrice", 1000m))));

        while (reader.Read())
        {
            Console.WriteLine($"  #{reader.GetInt32(0)}: {reader.GetString(1)} - ${reader.GetDecimal(2):F2}");

        // Execute a non-query (INSERT)
        var affected = manager.Execute(new DataStatement(
            "UPDATE Production.Product SET ListPrice = ListPrice * 1.05 WHERE ProductID = @id",
            o => o.Parameters = Arguments.ToArrayOf(new SqlParameter("@id", 999))));

        Console.WriteLine($"Rows affected: {affected}");

}}
}

Constructors

Name Description
SqlDataManager(Action<DataManagerOptions>)

Initializes a new instance of the SqlDataManager class.

Properties

Name Description
TransientFaultHandlingOptionsCallback

Gets or sets the callback delegate that will provide options for transient fault handling.

Methods

Name Description
Clone()

Creates a new object that is a copy of the current instance.

ExecuteCommand<T>(DataStatement, Func<IDbCommand, T>)

Core method for executing methods on the DbCommand object resolved from the virtual execute command workflow on DataManager.

ExecuteIdentityDecimal(DataStatement)

Executes the command statement and returns an identity value as decimal.

ExecuteIdentityInt32(DataStatement)

Executes the command statement and returns an identity value as int.

ExecuteIdentityInt64(DataStatement)

Executes the command statement and returns an identity value as long.

GetDbCommand(DataStatement)

Gets the command object used by all execute related methods.