#region License
// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
// This source code is made available under the terms of the GNU Lesser General Public License v3.0
// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
#endregion
using System;
using System.Data.Common;
using System.Data.SqlClient;
namespace MiniSqlQuery.Core
{
///
/// The sql query runner.
///
public class SqlQueryRunner : QueryRunner
{
///
/// Initializes a new instance of the class.
///
/// The provider factory.
/// The connection string.
/// The enable query batching.
///
public SqlQueryRunner(DbProviderFactory factory, string connectionString, bool enableQueryBatching, int commandTimeout)
: base(factory, connectionString, enableQueryBatching, commandTimeout)
{
}
///
/// The handle batch exception.
///
/// The db exception.
protected override void HandleBatchException(DbException dbException)
{
Exception = dbException;
var exp = dbException as SqlException;
if (exp != null)
{
foreach (SqlError error in exp.Errors)
{
Messages += string.Format("Error line {0}: {1}.{2}", error.LineNumber, error.Message, Environment.NewLine);
}
}
}
///
/// The subscribe to messages.
///
/// The connection.
protected override void SubscribeToMessages(DbConnection connection)
{
var conn = connection as SqlConnection;
if (conn != null)
{
conn.InfoMessage += ConnectionInfoMessage;
}
}
///
/// The unsubscribe from messages.
///
/// The connection.
protected override void UnsubscribeFromMessages(DbConnection connection)
{
var conn = connection as SqlConnection;
if (conn != null)
{
conn.InfoMessage -= ConnectionInfoMessage;
}
}
///
/// The connection information message collection method.
///
/// The sender.
/// The events for the message.
private void ConnectionInfoMessage(object sender, SqlInfoMessageEventArgs e)
{
Messages += e.Message + Environment.NewLine;
}
}
}