-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableValuedParameter.cs
50 lines (45 loc) · 1.87 KB
/
TableValuedParameter.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
using System.Data;
namespace Dapper
{
/// <summary>
/// Used to pass a DataTable as a TableValuedParameter
/// </summary>
internal sealed class TableValuedParameter : SqlMapper.ICustomQueryParameter
{
private readonly DataTable table;
private readonly string typeName;
/// <summary>
/// Create a new instance of <see cref="TableValuedParameter"/>.
/// </summary>
/// <param name="table">The <see cref="DataTable"/> to create this parameter for</param>
public TableValuedParameter(DataTable table) : this(table, null) { /* run base */ }
/// <summary>
/// Create a new instance of <see cref="TableValuedParameter"/>.
/// </summary>
/// <param name="table">The <see cref="DataTable"/> to create this parameter for.</param>
/// <param name="typeName">The name of the type this parameter is for.</param>
public TableValuedParameter(DataTable table, string typeName)
{
this.table = table;
this.typeName = typeName;
}
void SqlMapper.ICustomQueryParameter.AddParameter(IDbCommand command, string name)
{
var param = command.CreateParameter();
param.ParameterName = name;
Set(param, table, typeName);
command.Parameters.Add(param);
}
internal static void Set(IDbDataParameter parameter, DataTable table, string typeName)
{
#pragma warning disable 0618
parameter.Value = SqlMapper.SanitizeParameterValue(table);
#pragma warning restore 0618
if (string.IsNullOrEmpty(typeName) && table != null)
{
typeName = table.GetTypeName();
}
if (!string.IsNullOrEmpty(typeName)) StructuredHelper.ConfigureTVP(parameter, typeName);
}
}
}