-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDapperRepository.cs
112 lines (89 loc) · 3.87 KB
/
DapperRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dapper_Example
{
public class DapperRepository
{
//step 1: install these packages :
// - Dapper
// -System.Data.SqlClient;
// if you are running a simple peice of sql with some passed in arguments then do it like this with no response
public async Task<bool> RunSqlQuery(long locationId, int employeeId, DateTime timeStamp)
{
try
{
using (var connection = new SqlConnection("SQL CONNECTION STRING GOES HERE "))
{
var sql = $"SELECT * FROM <SOME TABLE> WHERE LocationId='{locationId}'" ;
var values = new { Location_ID = locationId, Employee_ID = employeeId, Datetime = timeStamp };
var result = await connection.ExecuteAsync(sql);
return true;
}
}
catch (SqlException ex)
{
return false;
}
}
//If you are pulling from a stored Procedure then you would call it with something like this
public async Task<bool> CallStoredProcedure(long locationId, int employeeId, DateTime timeStamp)
{
try
{
using (var connection = new SqlConnection("SQL CONNECTION STRING GOES HERE "))
{
var procedure = "[stocktake].[PR_Stocktake_CompleteFirstCount]";
var values = new { Location_ID = locationId, Employee_ID = employeeId, Datetime = timeStamp };
var result = await connection.ExecuteAsync(procedure, values, commandType: CommandType.StoredProcedure);
return true;
}
}
catch (SqlException ex)
{
return false;
}
}
// if you get rows of data back from sql then make an object that matches the columns and we can return the data as a list of that
public async Task<List<returnobject>> RunSqlQuerywithObjectReturned(long locationId, int employeeId, DateTime timeStamp)
{
try
{
using (var connection = new SqlConnection("SQL CONNECTION STRING GOES HERE "))
{
var sql = $"SELECT * FROM <SOME TABLE> WHERE LocationId='{locationId}'";
var values = new { Location_ID = locationId, Employee_ID = employeeId, Datetime = timeStamp };
var result = await connection.QueryAsync<returnobject>(sql);
return result.ToList();
}
}
catch (SqlException ex)
{
return null;
}
}
//If you are pulling from a stored Procedure then you would call it with something like this
public async Task<List<returnobject>> CallStoredProcedureWithObjectReturned (long locationId, int employeeId, DateTime timeStamp)
{
try
{
using (var connection = new SqlConnection("SQL CONNECTION STRING GOES HERE "))
{
var procedure = "[stocktake].[PR_Stocktake_CompleteFirstCount]";
var values = new { Location_ID = locationId, Employee_ID = employeeId, Datetime = timeStamp };
var result = await connection.QueryAsync<returnobject>(procedure, values, commandType: CommandType.StoredProcedure);
return result.ToList();
}
}
catch (SqlException ex)
{
return null;
}
}
}
}