Click here to Skip to main content
15,943,061 members
Articles / Database Development / SQL Server / SQL Server 2014

Another C# SQL class

Rate me:
Please Sign up or sign in to vote.
4.80/5 (24 votes)
1 Apr 2014CPOL4 min read 48.8K   1.7K   47   15
Another article on C# .NET SQL, but with smarts


Like most other business developers, I've written my fair share of programs that do some form or another of database interaction. Load this chunk of data, transform it, shove it over here. Or, the other, load this chunk of data with these filters, render it for editing and save the changes back. Every business developer, at some point, will do some form or variation of these steps.

However, how we go about doing it seems to be a little too different. I mean .NET has the awesome System.Data.SqlClient namespace to really handle our SQL Server interaction needs. However, it is really low level to me. It requires a lot of setup JUST to get something going:

// I mean, look at the setup JUST to get this going:

var con = new SqlConnection("connectionString");
var cmd = new SqlCommand()
    CommandText = "SELECT col_one FROM tb_table WHERE col_id = @id",
    CommandType = CommandType.Text,
    CommandTimeout = 30, // Optional, but included for the sake of argument
    Connection = con

var datareader = cmd.ExecuteReader(true); // close the connection

// Now have fun converting the Reader to a DataSet

To me, that just seems like a lot of work upfront to try and take care of just querying for some information. However, just like me, I'm pretty sure most developers have some sort of class that abstracts some of this away. Well, today, I'm presenting to you all my solution: SQLMagic.

What is SQLMagic?

SQLMagic started as a small project to simplify my C# code so I can focus on writing the queries once (in your flavor of editor. I prefer SQL Server Management Studio), drop them into my application, execute them, and keep going. I don't want to spend too much time setting up my application environment to interact with my SQL Server. I want my simple class that I reference, create, and use forever. Thus, SQLMagic was born.


SQLMagic was designed to be simple. There should be no guesswork as to what SQLMagic is doing, ever. It should be able to handle anything I need it to without raising all sorts of fussiness other than what SQL Server would normally throw back. As such, SQLMagic has a rather large collection of ways to achieve that goal. 99% of you will end up using this in a simple manner without the need to go further. However, this article will have a more advanced section, just in case.

Using the code

SQLMagic is simple. That was the point. As such, using it is simple:

// Declare once, keep using
var nTimeout = 30; // timeout
var bLog = true;

// Create with a timeout of 30 seconds and logging as true
var oSql = new Sql("Connection String", nTimeout, bLog);

// Now let's roll!
SqlResultWithDataSet oResult = oSql.Open("SELECT * FROM tbl");

That's it. That's all you need to do to open a DataSet!

But, if you listen to most DBA's (and developers with common sense), you'll realize you really should be using parameters in your queries. Well, don't worry, SQLMagic has you covered:

// I'm going to re-use oSql and oResult from above ^^^

oResult = oSql.Open("SELECT * FROM tbl WHERE tbl_id = @id",
    new SqlParameter("id", 100)

SQLMagic makes it incredibly simple to write proper queries:

oResult = oSql.Open("SELECT * FROM tbl WHERE tbl_id IN (@id1, @id2, @id3)",
    new SqlParameter("id1", 1),
    new SqlParameter("id2", 2),
    new SqlParameter("id3", 3)

It doesn't matter how many parameters you need. SQLMagic makes it easy and effortless.

That covers most of the use cases. However, SQLMagic, since it utilizes the SqlClient namespace, allows for so much more:

// Stored Procedures with parameters?
    new SqlParameter("param1", "value!")

// What about return values?
var datetime = oSql.Execute<DateTime>("SELECT GETDATE()");
var intBack = oSql.Execute<Int32>("SELECT 1");

// Stored Procedure that has a return value?
var intBackAgain = oSql.Execute<Int32>("sp_GiveNumber", CommandType.StoredProcedure);

SQLMagic makes all of this incredibly simple and easy to use.

Power Programmers: Using the code

This section helps expose a little more of the features that SQLMagic encompasses. This doesn't necessarily mean these subjects are difficult, but they are definitely what more advanced developers would be using.

Asynchronous Support

SQLMagic, with compilation flag "NET45" set, supports the async/await model of .NET, and implements it in a very clean, easy to use manner:

// Converting is VERY easy!
var oResult = await oSql.OpenAsync("SELECT * FROM aVeryLargeTable");

var nValue = await oSql.ExecuteAsync<Int32>("SELECT bigNumberComputationThatResultsInInteger");

var nProcedure = await oSql.ExecuteAsync<Int32>("sp_LongRunningProcedure",

Done! You are now using the async/await functionality of .NET 4.5+. SQLMagic supports the exact same overloads (in proper order) in synchronous or asynchronous, just by changing a few lines of code.

Transaction Support

SQLMagic gives you the ability to Begin, Commit, and RollBack transactions with and without asychronous capability:

// Start a Transaction:
SqlTransaction oTransaction = oSql.BeginTransaction();

// The asynchronous version:
SqlTransaction oTransaction = await oSql.BeginTransactionAsync();

// one of the overloads that SQLMagic has is the ability to specify a transaction!
// public SqlResultWithDataSet Open(Statement aStatement, SqlConnection aConnection, Boolean aCloseConnection, SqlTransaction aTransaction)
// Statement is merely a SQLMagic struct that groups up some parameters from earlier:
Statement oStatement = new Statement
    Sql = "INSERT INTO tbl VALUES(@val1)",
    Type = CommandType.Text,
    Parameters = new List<SqlParameters>() { new SqlParameter("val1", "value") }
oSql.Open(oStatement, oTransaction.Connection, false, oTransaction);

// Open a DataSet of the given statement, connection, don't close it, and use that transaction

// Once you're all done, you end!
oSql.EndTransaction(oTransaction, true); // true or false indicates COMMIT or ROLLBACK!

// The asynchronous version:
await oSql.EndTransactionAsync(oTransaction);

Manual Connection Creation

SQLMagic lets you create connections yourself (remember that SQLMagic has overloads to indicate a SqlConnection object!):

// Synchronous
SqlConnection oConnection = oSql.CreateConnection(true);

// Asynchronous
SqlConnection oConnection = await oSql.CreateConnectionAsync(true);

// The boolean value indicates whether or not the connection is automatically opened when created


SQLMagic can attempt to execute a query for you and, instead of throwing an exception, will simply return a Boolean value that indicates success, and use an out parameter to store your result:

// Variables
SqlResultWithDataSet oResult;
if (oSql.TryOpen("SELECT * FROM tbl WHER = 1", out oResult))
    // This fails because of syntax   ^
    // Because it failed, you can handle the exception here!

What's Next?

There's still a few things I want to accomplish with SQLMagic. For starters, a Fluent-like interface would be pretty nifty:

// This would be somewhat
var oResult = 
    new SqlMagic("connectionString")
    .Select("column1", "column2")
    .Where("x", Operators.GreaterThan, "1")
    .And("y", Operators.LessThan, "2")

However that hasn't been fully considered because I don't want to tread on or replace what LINQ-To-SQL offers.

I've also started on an observer class that records its observations of various commands and connection activity. It lives inside SQLMagic's main class as a private, static class. That is subject to change, though.

Breaking Changes

This library could undergo breaking changes to either incorporate new functionality or to normalize namespaces. This is just a small warning.

Points of Interest

async/await was a little weird to implement at first. I kept having programs lock up on me. Context capturing is difficult sometimes.

My co-worker has been a DBA before and he helped me make sure the public facing API made sense, and has been communicating with me what features I should add, or what seem useful.

SQLMagic also has a dependancy on a NuGet package: The immutable collections library by Microsoft.


4/1/2014: Initial Publish 1.0


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United States United States
I am a professional Software Developer. I work primarily in C# and interact with Microsoft SQL Server at my job. I have been developing software since I was 15 and continue to learn more and more every day.

Comments and Discussions

PraiseThank you Pin
SaraCoder13-Apr-18 6:14
SaraCoder13-Apr-18 6:14 
Questionpossible exception Pin
Herman<T>.Instance29-Apr-15 0:34
Herman<T>.Instance29-Apr-15 0:34 
QuestionNice learning stuff~ Pin
wsc091811-Dec-14 19:05
wsc091811-Dec-14 19:05 
QuestionWhy not *any* ORM? Pin
tmenier8-Oct-14 4:48
tmenier8-Oct-14 4:48 
AnswerRe: Why not *any* ORM? Pin
Herman<T>.Instance29-Apr-15 0:35
Herman<T>.Instance29-Apr-15 0:35 
GeneralRe: Why not *any* ORM? Pin
Mel Padden10-Mar-17 1:23
Mel Padden10-Mar-17 1:23 
Questionwhy not dapper? Pin
Rahman Mahmoodi8-Apr-14 22:11
Rahman Mahmoodi8-Apr-14 22:11 
AnswerRe: why not dapper? Pin
formlesstree49-Apr-14 21:03
professionalformlesstree49-Apr-14 21:03 
QuestionI created a very similar object a lot of time ago... Pin
Menci Lucio4-Apr-14 0:40
professionalMenci Lucio4-Apr-14 0:40 
AnswerRe: I created a very similar object a lot of time ago... Pin
formlesstree44-Apr-14 6:57
professionalformlesstree44-Apr-14 6:57 
GeneralRe: I created a very similar object a lot of time ago... Pin
Menci Lucio7-Apr-14 6:09
professionalMenci Lucio7-Apr-14 6:09 
GeneralRe: I created a very similar object a lot of time ago... Pin
formlesstree47-Apr-14 11:27
professionalformlesstree47-Apr-14 11:27 
GeneralRe: I created a very similar object a lot of time ago... Pin
Menci Lucio8-Apr-14 5:02
professionalMenci Lucio8-Apr-14 5:02 
GeneralGot My Vote Pin
cjb1101-Apr-14 21:11
cjb1101-Apr-14 21:11 
GeneralRe: Got My Vote Pin
formlesstree42-Apr-14 4:02
professionalformlesstree42-Apr-14 4:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.