Skip to content

Operation

Fúlvio edited this page Jan 23, 2018 · 7 revisions

Operation

1. Connection

1.1. MySql

string strconn = "Server=localhost;Database=testdb;Uid=root;Pwd=senha;SslMode=none";
MySqlConnection connection = new MySqlConnection(strconn);

1.2. Postgres

string strconn = "Server=127.0.0.1;Port=5432;Database=postgres;User Id=postgres;Password=senha;";    
NpgsqlConnection connection = new NpgsqlConnection(strconn);

1.3. SqlServer

string strconn = "Server=.\\SqlExpress;Database=Test;User Id=sa;Password=senha;";
SqlConnection connection = new SqlConnection(strconn);

2. Table

CREATE TABLE [Credit](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Description] [varchar](50) NOT NULL,
	[Created] [date] NULL,
 CONSTRAINT [PK_Credit] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]

3. Model

public class Credit
{
	public Credit() { }		
	public int Id { get; set; }
	public string Description { get; set; }
	public DateTime? Created { get; set; }
}

4. Insert

var id = connection
    .SoftBuild()
    .From("Credit")
    .AsInsert(new Dictionary<string, object>
    {
        ["Description"] = credit.Description,
        ["Created"] = credit.Created
    }) // Insert( ... ) old version
    .SaveInsert<int>();

Note: the id contains the value entered in the bank in the auto increment field.

5. Update

connection
	.SoftBuild()
	.From("Credit")
	.Where("Id", credit.Id)
	.AsUpdate(new Dictionary<string, object>
	{
		["Description"] = credit.Description,
		["Created"] = credit.Created
	}) // Update( ... ) old version
	.SaveUpdate();

6. Delete

connection
	.SoftBuild()
	.From("Credit")
	.Where("id", id)
	.AsDelete() //Delete() old version
	.SaveUpdate();

7. Find

connection
	.SoftBuild()
	.From("Credit")
	.Where("Id", id)
	.FindOne<Credit>()

8. List

IEnumerable<Credit> model = connection
            .SoftBuild()
            .From("Credit")
            .OrderBy("Description")                
            .List<Credit>();
Clone this wiki locally