GithubHelp home page GithubHelp logo

iag-unity-dataaccess's Introduction

IAG-Unity-DataAccess

NuGet Package Page

https://www.nuget.org/packages/Iag.Unity.DataAccess.dll/

First things first. Initialize your data library:

DataLibrary.Initialize("<Your connection string here>");

Initializing the DataLibrary will let you avoid having to specify a connection string or SqlConnection on each StoredProcedure or UnitySqlCommand object. If you elect to not generally initialize the connection (possibly because you are managing multiple connections) the constructors for StoredProcedure and UnitySqlCommand take a SqlConnection as the first parameter:

using (SqlConnection conn1 = new SqlConnection("<Your connection string here>"))
using (SqlConnection conn2 = new SqlConnection("<Your connection string here>"))
using (StoredProcedure sp = new StoredProcedure(conn1, "<Your procedure name>")) 
using (UnitySqlCommand cmd = new UnitySqlCommand(conn2 "<Your procedure name>"))
{
     // The data access library takes care of opening the SqlConnection object if it isn't already open.
     ...
}

Executing a stored procedure without parameters (returning a single table):

using (StoredProcedure sp = new StoredProcedure("<Your procedure name>"))
{
     DataTable tbl = sp.OpenTable();
}

Executing a stored procedure with parameters (returning a single table):

using (StoredProcedure sp = new StoredProcedure("<Your procedure name>"))
{
     sp.Prepare(); // Initialize the parameter dictionaries, etc.
     sp.Parameters["@Id"] = 123;
     DataTable tbl = sp.OpenTable();
}

Executing a sql command without parameters (returning a single table):

using (UnitySqlCommand cmd = new UnitySqlCommand("<Your sql statement>"))
{
     DataTable tbl = cmd.OpenTable();
}

Executing a stored procedure with parameters (returning a single table):

using (UnitySqlCommand cmd = new UnitySqlCommand("<Your sql statement WHERE Id = @Id>"))
{
     cmd.Prepare();
     cmd.Parameters["@Id"] = 123;
     DataTable tbl = cmd.OpenTable();
}

Utilizing GetObject()

The GetObject() function allows you to take a single table and convert them into a list of objects. This is a simple implementation that currently does not support collection properties automatically filled with a child data. There are ways the GetObject() method can be used:

Simply mapping field name in the data table to property name

This example assumes that the object to be loaded with data has exact matching of the field name to the property. The method can be invoked using an optional parameter indicating whether to evaluate property and field names in a case-insensitive or case-sensitive manner.

using (UnitySqlCommand sp = new UnitySqlCommand("SELECT CAST('2015-12-12 12:12:12 -08:00' as datetimeoffset) as 'DateTimeOffsetValue', 'string' as StringValue, 123 as IntValue, 12.34 as DoubleValue, '2015-10-12 12:12:44' as DateTimeValue, 1 as BoolValue"))
{
	/// This implementation will evaluate field->property mapping in a case-insensitive manner.
	List<TestLoadClass> tlc1 = sp.GetObjects<TestLoadClass>().ToList();
	
	/// This implementation will evaluate field->property mapping in a case-sensitive manner.
	List<TestLoadClass> tlc2 = sp.GetObjects<TestLoadClass>(true).ToList();
}

Utilizing the c#FieldToProperty attribute

In this case, the target object class definition has properties decorated with c#[FieldToProperty('<fieldName>')].

public class TestLoadClassMapped
{
	[FieldToProperty("STR")]
	public string StringValue { get; set; }
	[FieldToProperty("INT")]
	public int? IntValue { get; set; }
	[FieldToProperty("DBL")]
	public double? DoubleValue { get; set; }
	[FieldToProperty("DT")]
	public DateTime? DateTimeValue { get; set; }
	[FieldToProperty("DTO")]
	public DateTimeOffset? DateTimeOffsetValue { get; set; }
	[FieldToProperty("B")]
	public bool? BoolValue { get; set; }
}

using (UnitySqlCommand sp = new UnitySqlCommand("SELECT 'string' as STR, 123 as INT, 12.34 as DBL, CAST('2015-10-12 12:12:44' as DateTime) as DT, CAST('2015-12-12 12:12:12 -08:00' as DateTimeOffset) as DTO, 0 as B"))
{
	List<TestLoadClassMapped> tlc = sp.GetObjects<TestLoadClassMapped>().ToList();
}

Utilizing a custom mapping function

Create a function that receives a string parameter that indicates the table's field name to be mapped. The result of the function is to return the property name to be populated.

using (UnitySqlCommand sp = new UnitySqlCommand("SELECT 'string' as STR, 123 as INT, 12.34 as DBL, CAST('2015-10-12 12:12:44' as DateTime) as DT, CAST('2015-12-12 12:12:12 -08:00' as DateTimeOffset) as DTO, 0 as B"))
{

	Func<string, string> func = (x) =>
	{
		if (x == "STR") return "StringValue";
		else if (x == "INT") return "IntValue";
		return null;
	};

	List<TestLoadClassNullable> tlc = sp.GetObjects<TestLoadClassNullable>(func).ToList();
}

Other return types and execution methods for StoredProcedure and UnitySqlCommand:

void Execute();
DataSet OpenDataSet([name = null]);
IEnumerable<DataRow> GetRows();
IEnumerable<IEnumerable<DataRow>> GetRowSets();
object ExecuteScalar();
T ExecuteScalar<T>(T defaultValue);
SqlDataReader ExecuteReader(CommandBehavior commandBehavior = CommandBehavior.Default);

The following calls have been marked [Obsolete]. Use ExecuteScalar<T>() instead.

bool ExecuteScalar(bool defaultValue = false);
byte[] ExecuteScalar(byte[] defaultValue = null);
DateTime? ExecuteScalar(DateTime? defaultValue = null);
long ExecuteScalar(long defaultValue = null);
int ExecuteScalar(int defaultValue = null);
string ExecuteScalar(string defaultValue = null);

Executing within a transaction

using (UnitySqlCommand cmd1 = new UnitySqlCommand("<Your sql statement>"))
using (UnitySqlCommand cmd2 = new UnitySqlCommand("<Your sql statement>"))
using (UnitySqlCommand cmd3 = new UnitySqlCommand("<Your sql statement>"))
{
    SqlTransaction trans = null;
    try
    {
         cmd1.Prepare();
         trans = cmd1.BeginTransaction();
         cmd2.Prepare(trans); // Prepare with the transaction
         cmd3.Prepare(trans); // Prepare with the transaction
         
         cmd1.Execute();
         cmd2.Execute();
         cmd3.Execute();
         trans.Commit();
    }
    catch
    {
       trans?.Rollback();
    }
}

Or utilize the CreateTransaction() method. This method will take all the commands/procedures passed to it and set them up with the same connection and into the same transaction. DO NOT Prepare() after creating the transaction! The CreateTransaction() method will Prepare() for you. After the CreateTransaction(), you can set any parameters necessary.

using (UnitySqlCommand cmd1 = new UnitySqlCommand("<Your sql statement>"))
using (UnitySqlCommand cmd2 = new UnitySqlCommand("<Your sql statement>"))
using (UnitySqlCommand cmd3 = new UnitySqlCommand("<Your sql statement>"))
using (UnitySqlCommand cmd4 = new UnitySqlCommand("<Your sql statement>"))
{
     SqlTransaction trans = null;
     try
     {
	  trans = UnitySqlCommand.CreateTransaction(cmd1, cmd2, cmd3, cmd4);
          cmd1.Execute();
          cmd2.Execute();
          cmd3.Execute();
          cmd4.Execute();
	  trans.Commit();
     }
     catch
     {
          trans?.Rollback();
     }
}

License

The MIT License (MIT) Copyright (c) 2016 IAG Unity, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

iag-unity-dataaccess's People

Contributors

splauer1us avatar battousai999 avatar stevelauer avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.