This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 })) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE PROCEDURE [dbo].[sp_get_customer_orders] | |
( | |
@CustomerId INT | |
) | |
AS | |
BEGIN | |
SELECT * | |
FROM [dbo].[Orders] | |
WHERE (CustomerId = @CustomerId) | |
ORDER BY OrderDateUtc ASC; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var connection = new SqlConnection(connectionString)) | |
{ | |
var orders = connection.ExecuteQuery<Order>("[dbo].[sp_get_customer_orders]", | |
new { CustomerId = 10045 }, | |
commandType: CommandType.StoredProcedure); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
OlderNewer