What is the SqlDataAccess Component?

The SqlDataAccess class is a very lightweight wrapper around ADO.NET that aims to make it as simple as possible to fire direct database commands and return results without having to deal with setting up and opening connections explicitly. Most operations involve creating an instance of the class and passing in a connection string, and then using single method commands to fire against the database to return or manipulate data.

There's no magic here - this library for the most part is simply a very small abstraction ontop of ADO.NET to combine the typical series of commands into simple method calls. The component automatically opens and closes connections (where appropriate) and optionally captures all errors and reports them as part of an ErrorMessage property.

Connection Management

The class maintains a connection property and a provider name, which are used to open the connection, perform the data operation and close the connection.

The exception are DataReader and IEnumerable results which require that the connection stays open, because the readable DataReader or IEnumerable object that is created as data is read is returned to the consumer. In those cases the consumer is responsible for closing the connection. The best way to ensure that the connections are closed is to use using blocks around SqlDataAccess instances

using (var data = new SqlDataAccess("WebStoreConnection"))
{
    var reader = data.ExecuteReader("select * from customers");

    Assert.IsTrue(reader.HasRows);
                
    while (reader.Read())
    {
        Console.WriteLine((string)reader["LastName"] + " " + (DateTime)reader["Entered"]);
    }
}


© West Wind Technologies, 1996-2016 • Updated: 12/19/15
Comment or report problem with topic