Skip to content

Instantly share code, notes, and snippets.

@kironroy
Last active February 9, 2020 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kironroy/acb98aaeaa433e44c268f73055752fc5 to your computer and use it in GitHub Desktop.
Save kironroy/acb98aaeaa433e44c268f73055752fc5 to your computer and use it in GitHub Desktop.
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