Skip to content

Instantly share code, notes, and snippets.

@zippy1981
Created October 15, 2012 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zippy1981/3892203 to your computer and use it in GitHub Desktop.
Save zippy1981/3892203 to your computer and use it in GitHub Desktop.
C# RAISERROR Wrapper.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
public partial class SqlRaisError
{
/// <summary>
/// A SQLCLR wrapper method RAISERROR().
/// </summary>
/// <param name="message">The message raised by RAISERROR()</param>
/// <param name="severity">The message severity.</param>
/// <param name="state">The message state.</param>
public static void RaisError(SqlConnection cn, string message, short severity = (short)0, short state = (short)1)
{
RaisError(cn, message, severity, state);
}
/// <summary>
/// A SQLCLR wrapper method RAISERROR().
/// </summary>
/// <param name="message">The message raised by RAISERROR()</param>
/// <param name="severity">The message severity.</param>
/// <param name="state">The message state.</param>
/// <param name="args">These are the <c>printf()</c> style arguments used as substitution parameters for <paramref name="message"/>.</param>
/// <seealso cref="http://stackoverflow.com/a/337792/95195">Canonical StackOverflow answer on parameterizing multi-string answers.</seealso>
public static void RaisError(string message, short severity, short state, params object[] args)
{
using (var cn = new SqlConnection("context connection=true"))
using (var cmd = cn.CreateCommand())
{
cn.Open();
cmd.Parameters.AddWithValue("@msg", message);
cmd.Parameters.AddWithValue("@severity", severity);
cmd.Parameters.AddWithValue("@state", state);
if (args == null)
{
cmd.CommandText = "RAISERROR (@msg, @severity, @state)";
}
else
{
string[] argNames = new string[args.Length];
for (int i = 0; i < args.Length; i++)
{
argNames[i] = string.Format("@arg{0}", i);
cmd.Parameters.AddWithValue(argNames[i], args[i]);
}
cmd.CommandText = string.Format("RAISERROR (@msg, @severity, @state, {0})", string.Join(", ", argNames));
}
SqlContext.Pipe.ExecuteAndSend(cmd);
cn.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment