Last active
February 9, 2020 23:21
-
-
Save kironroy/acb98aaeaa433e44c268f73055752fc5 to your computer and use it in GitHub Desktop.
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 Dapper; | |
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Data.SqlClient; | |
using System.Linq; | |
using System.Text; | |
namespace DataAccessLibrary | |
{ | |
public class SqlDataAccess | |
{ | |
// Create a connection to our database with a using statement | |
// using statement closes the connection to the database out properly | |
// Query is a Dapper element | |
// "For every row, Dapper will put into this model type" | |
// Convert to a List -> .ToList() method | |
public List<T> LoadData<T, U>(string sqlStatement, U parameters, string connectionString) | |
{ | |
using (IDbConnection connection = new SqlConnection(connectionString)) | |
{ | |
// Query is a read | |
List<T> rows = connection.Query<T>(sqlStatement, parameters).ToList(); | |
return rows; | |
} | |
} | |
public void SaveData<T>(string sqlStatement, T parameters, string connectionString) | |
// there will be parameters in this method | |
{ | |
using (IDbConnection connection = new SqlConnection(connectionString)) | |
{ | |
// Execute is a write | |
connection.Execute(sqlStatement, parameters); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment