Skip to content

Instantly share code, notes, and snippets.

View mikependon's full-sized avatar
Stop, do not knock! I am busy with RepoDB!

Michael Camara Pendon mikependon

Stop, do not knock! I am busy with RepoDB!
View GitHub Profile
@mikependon
mikependon / IDbConnectionExecuteQueryPerson.cs
Last active June 1, 2019 07:21
IDbConnection (ExecuteMethods for Person)
// Query
using (var connection = new SqlConnection(connectionString))
{
connection.ExecuteQuery<Person>("SELECT * FROM [Person] WHERE Id = @Id;", new { Id = 10045 });
}
// Insert
using (var connection = new SqlConnection(connectionString))
{
var entity = new
@mikependon
mikependon / IDbConnectionQueryPerson.cs
Last active May 31, 2019 08:03
IDbConnection (Query<Person>)
// Query
using (var connection = new SqlConnection(connectionString))
{
var person = connection.Query<Person>(p => p.Id == 10045).First();
}
// Insert
using (var connection = new SqlConnection(connectionString))
{
var person = new Person
@mikependon
mikependon / IDbConnectionInsertAllOrders.cs
Last active May 30, 2019 13:12
IDbConnection (InsertAll<Orders>)
// Create a list
var orders = new List<Order>();
// Add each item
orders.Add(new Order { CustomerId = 10045, Quantity = 1, ProductId = 24 });
orders.Add(new Order { CustomerId = 10045, Quantity = 4, ProductId = 27 });
// More orders here
// Call the InsertAll
@mikependon
mikependon / IDbConnectionBulkInsertOrders.cs
Last active May 30, 2019 13:10
IDbConnection (BulkInsert<Order>)
// Create the list
var orders = new List<Order>();
// Add each item
orders.Add(new Order { CustomerId = 10045, Quantity = 1, ProductId = 24 });
orders.Add(new Order { CustomerId = 10045, Quantity = 4, ProductId = 27 });
// More orders here
// Call the BulkInsert
@mikependon
mikependon / IDbConnectionExecuteQueryAndBulkInsert.cs
Created May 29, 2019 04:23
IDbConnection (ExecuteQuery and BulkInsert)
// Variables for paging
var lastId = 10000;
var batchSize = 1000000;
var page = 1;
// Create a source connection (Oracle)
using (var sourceConnection = new OracleConnection(sourceConnectionString))
{
// Read the data with target columns
using (var reader = sourceConnection.ExecuteReader("SELECT Id, FirstName, LastName, LastUpdatedUtc, DateInsertedUtc FROM [dbo].[Person] WHERE Id > @Id OFFSET @Offset FETCH NEXT @BatchSize ROWS ONLY;", new { Id = lastId, Offset = (batchSize * page), BatchSize = batchSize }))
@mikependon
mikependon / StoredProcedureGetCustomerOrders.sql
Created May 29, 2019 04:24
Stored Procedure (GetCustomerOrders)
CREATE PROCEDURE [dbo].[sp_get_customer_orders]
(
@CustomerId INT
)
AS
BEGIN
SELECT *
FROM [dbo].[Orders]
WHERE (CustomerId = @CustomerId)
ORDER BY OrderDateUtc ASC;
@mikependon
mikependon / IDbConnectionExecuteQueryOrdersViaStoredProcedure.cs
Last active May 30, 2019 13:11
IDbConnection (ExecuteQuery<Orders> via StoredProcedures)
using (var connection = new SqlConnection(connectionString))
{
var orders = connection.ExecuteQuery<Order>("[dbo].[sp_get_customer_orders]",
new { CustomerId = 10045 },
commandType: CommandType.StoredProcedure);
}
@mikependon
mikependon / IDbConnectionExecuteQueryMultiple.cs
Created May 29, 2019 04:29
IDbConnection (ExecuteQueryMultiple)
using (var connection = new SqlConnection(connectionString))
{
// Call the method by passing multiple statements
var extractor = connection.ExecuteQueryMultiple("SELECT * FROM [dbo].[Customer] WHERE Id = @CustomerId; " +
"SELECT * FROM [dbo].[Order] WHERE CustomerId = @CustomerId; " +
"SELECT GETUTCDATE() AS RetrievedDateTimeUtc;",
new { CustomerId = 10045 });
// Extract the results
var customer = extractor.Extract<Customer>().FirstOrDefault();
@mikependon
mikependon / IDbConnectionExecuteQueryMultipleWithStoredProcedure.cs
Created May 29, 2019 04:31
IDbConnection (ExecuteQueryMultiple with StoredProcedure)
using (var connection = new SqlConnection(connectionString))
{
// Call the method by passing multiple statements
var extractor = connection.ExecuteQueryMultiple("SELECT * FROM [dbo].[Customer] WHERE Id = @CustomerId; " +
"EXEC [dbo].[sp_get_customer_orders] @CustomerId; " +
"SELECT GETUTCDATE() AS RetrievedDateTimeUtc;",
new { CustomerId = 10045 });
// Extract the results
var customer = extractor.Extract<Customer>().FirstOrDefault();
@mikependon
mikependon / IDbConnectionQueryMultipleCustomerAndOrder.cs
Created May 29, 2019 04:33
IDbConnection (QueryMultiple<Customer, Order>)
using (var connection = new SqlConnection(connectionString))
{
// Call the method, returns Tuple<T1, T2>
var tuple = connection.QueryMultiple<Customer, Order>(c => c.Id == customerId, o => o.CustomerId = customerId);
// Get each item from the Tuple
var customer = tuple.Item1.FirstOrDefault();
var orders = tuple.Item2.ToList();
}