Basic Usage Instructions

Introduction

This is a simple tutorial that explains how you can use the data access library to load, save and delete a custom object from a Sql Server data source.

Details

The first thing you should do after downloading the latest release is add a reference to your project.

Once the reference has been added to your project, you will need to create a connection string in your app.config (desktop application) or web.config (web application). The connection should be created under the connectionStrings section and must have a valid connection string containing the connection data source provider. You can also pass the information directly to the assembly.

Sql Server Configuration Example

<add name="MyConnection" connectionString="Data Source=www.myserver.com;Initial Catalog=MyDatabase;User Id=MyUsername;Password=MyPassword;" providerName="System.Data.SqlClient" /> 


Once you have created your configuration string you must inform the data access library of the connection string you plan to use.

This is done by setting the ConnectionName property of the DataExpress.DataAccess class.

Once done, you can start to implement the required IDataServiceable* interface into any class that you plan to use the data access library with.

Example Class

using Delta.Data.DataExpress;
using System;
using System.Data;

namespace ExampleApplication
{
    public class Person : IDataServiceable
    {
        #region Constructors
        public Person()
        {
            this.Guid = Guid.NewGuid();
            this.Email = string.Empty;
        }
        #endregion

        #region Properties
        public Guid Guid { get; set; }
        public string Email { get; set; }
        #endregion

        #region Load Methods
        public static Person Load(string email)
        {
            return DataAccess.GetSingle<Person>("SELECT * FROM People WHERE Email=@Email", CommandType.Text, new Parameter("Email", email));
        }
        #endregion

        #region Delete Methods
        public void Delete()
        {
            DataAccess.NonQuery("DELETE FROM People WHERE Guid = @Guid", CommandType.Text, new Parameter("Guid", this.Guid));
        }
        #endregion

        #region Save Methods
        public void Save()
        {
            string query = "IF EXISTS (SELECT Guid FROM People WHERE Guid=@Guid)"
                + "UPDATE People SET"
                + "Email=@Email"
                + "WHERE Guid=@Guid"
                + "ELSE"
                + "INSERT INTO People VALUES (@Guid, @Email)"
                + "RETURN";

            DataAccess.NonQuery(query, CommandType.Text, this);
        }
        #endregion

        #region IDataServiceable Methods
        public void Fill(System.Data.IDataReader reader)
        {
            this.Guid = Helper.ToGuid(reader["Guid"]);
            this.Email = Helper.ToString(reader["Email"]);
        }

        public Parameters GetSaveParameters()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", this.Guid));
            parameters.Add(new Parameter("Email", this.Email));
            return parameters;
        }
        #endregion
    }
}


Notice in the above example that the Person class has a static load method. This method is used to instruct the data access layer that you intend to create a Person class and fill if from the data source.
Commands can be passed in the form of text queries or stored procedures.

The next thing to note is that because we have implemented the IDataServiceable interface, we are required to include a method called fill for populating the class from the data source.

The data access library also includes a Helper class that will allow you to convert data from the data source in to the required type such as string, Guid, int, etc.

Last edited Apr 23, 2014 at 12:33 PM by DeltaWolf7, version 11

Comments

xGs_Manco May 21, 2014 at 2:24 PM 
Hey man congrats for that, i have a question i am trying to use this data layer because i saw it interesting.
But i dont understand why from my webapplication i need make a reference to datalayer. it said because i'm using iserviceable...
i dont want to make this reference some to do???