Skip to content

Instantly share code, notes, and snippets.

@gabrielgreen
Created September 6, 2012 05:00
Show Gist options
  • Save gabrielgreen/3651440 to your computer and use it in GitHub Desktop.
Save gabrielgreen/3651440 to your computer and use it in GitHub Desktop.
SqlUtility
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Collections;
public static class SqlUtility
{
public static T ExecuteScalar<T>(string connStr, string query, params object[] queryParams)
{
using (var con = new SqlConnection(connStr))
using (var cmd = new SqlCommand(query, con))
{
var queue = new Queue(queryParams);
var matches = Regex.Matches(query, @"@\w+");
foreach (Match match in matches)
cmd.Parameters.AddWithValue(match.Value, queue.Dequeue());
con.Open();
T result = (T)cmd.ExecuteScalar();
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment