asakura89 / AyumiFx

Simple compilation of .NET FX minilibs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Databossy

Query your data like a boss. With simplicity and control.

How to

1. Open connection
using (var db = new Database())
{
    // .. query data
}

Call new Database without parameter, it will use System.Data.SqlClient provider.
Call with one parameter, it will open connection with connection string name as parameter.
And if you want to use connection string instead of name, you should call it with connection string and
ConnectionStringType as parameters

2. Get data in DataTable or Generic List it's your call
using (var db = new Database())
{
    return db.Query("SELECT * FROM Product");
}

or ...

using (var db = new Database())
{
    return db.Query<Product>("SELECT * FROM Product");
}

If you use Generic List, it will get all matching your class's properties or fields
It's all by convention
ex. you have table structure and class like below

CREATE TABLE Category
(
    [Id] VARCHAR(30),
    [Desc] VARCHAR(30)
)

CREATE TABLE Product
(
    [Id] VARCHAR(30),
    [Name] VARCHAR(100),
    CategoryId VARCHAR(30),
    Price DECIMAL
)
public class Category
{
    public String Id { get; set; }
    public String Desc { get; set; }
}

public class Product
{
    public String Id { get; set; }
    public String Name { get; set; }
    public String CategoryId { get; set; }
    public Decimal Price { get; set; }
}

so it will get Id, Name and CategoryId based on class properties.

You can do like this too.

public class ProductViewModel
{
    public String Id { get; set; }
    public String Name { get; set; }
    public String CategoryJ { get; set; }
}

using (var db = new Database())
{
    var query = new StringBuilder()
        .Append("SELECT p.[Id], p.[Name], c.[Desc] CategoryJ ")
        .Append("FROM Product p JOIN Category c ON c.[Id] = p.CategoryId ")
        .Append("WHERE p.[Id] = @0")
        .ToString();

    return db.QuerySingle<ProductViewModel>(query, prodId);
}
3. Execute Insert or Update

If you see above. you will get the idea quickly

String newCompanyId =  KeyBlaster.BuildSimpleKey(8, Keywielder.KeyBlaster.SimpleKeyType.ALPHANUMERIC);
db.Execute("INSERT INTO Company VALUES (@0, @1, @2, @3, @4, @5)",
    newCompanyId, txtName.Text, txtAddress.Text, txtCity.Text, txtPhoneNo.Text, txtCEOId.Text);

or ...

var query = new StringBuilder()
    .Append("UPDATE Company  ")
    .Append("SET companyName = @0, companyAddress = @1, ")
    .Append("city = @2, phone = @3, companyCEOId = @4 ")
    .Append("WHERE companyId = @5")
    .ToString();

db.Execute(query, txtName.Text, txtAddress.Text, txtCity.Text, txtPhoneNo.Text, txtCEOId.Text, txtCompanyId.Text);
4. Using named param instead

If you have ViewModel or say an object that hold your data to save, you just could drop it in like this

var query = new StringBuilder()
    .Append("UPDATE Company  ")
    .Append("SET companyName = @Name, companyAddress = @Address, ")
    .Append("city = @City, phone = @PhoneNo, companyCEOId = @CEOId ")
    .Append("WHERE companyId = @CompanyId")
    .ToString();

db.Execute(query,
    new
    {
        Name = txtName.Text,
        Address = txtAddress.Text,
        City = txtCity.Text,
        PhoneNo = txtPhoneNo.Text,
        CEOId = txtCEOId.Text,
        CompanyId = txtCompanyId.Text
    });

Keywielder

Simple token, unique key generator. It's one of my favorite library to generate formatted primary key. Such as Document number in (LOB) apps.

How to - 1

String key = Wielder
    .New()
    .AddString("KEY")
    .AddString("-")
    .AddLongYear()
    .AddNumericMonth()
    .AddDate()
    .AddString("-")
    .AddLeftPadded(w => w.AddCounter(12, 10), 4, '0')
    .BuildKey()
    .Dump();

Console.WriteLine(key);

Above code will generate key like this (below), result may vary as I use date KEY-20150125-0022

How to - 2

String complexKey = Wielder
    .New()
    .AddString("SIMPLE")
    .AddString("-")
    .AddGuidString()
    .AddString("-")
    .AddLongYear()
    .AddString("-")
    .AddRandomAlphaNumeric(10)
    .AddString("-")
    .AddRandomString(5)
    .AddString("-")
    .AddNumericDay()
    .BuildKey()
    .Dump();

Console.WriteLine(complexKey);

Above code will generate key like this (below), result may vary as I use year and combination random and GUID SIMPL-3d5968351a0645dda7bfd3bbeb4ad972-2015-CRFWSJWZ3B-SPRHG-01


NameValueItem

Simple implementation of Name-Value pair object

About

Simple compilation of .NET FX minilibs

License:The Unlicense


Languages

Language:C# 98.0%Language:ASP.NET 1.1%Language:Batchfile 0.4%Language:CSS 0.2%Language:HTML 0.2%