-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlServer.StoredProcedures.DbParameters.cs
More file actions
169 lines (158 loc) · 6.37 KB
/
Copy pathSqlServer.StoredProcedures.DbParameters.cs
File metadata and controls
169 lines (158 loc) · 6.37 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
namespace DBAClientX;
public partial class SqlServer
{
/// <summary>
/// Executes a stored procedure using an existing collection of <see cref="DbParameter"/> instances.
/// </summary>
public virtual object? ExecuteStoredProcedure(
string serverOrInstance,
string database,
bool integratedSecurity,
string procedure,
IEnumerable<DbParameter>? parameters = null,
bool useTransaction = false,
string? username = null,
string? password = null)
{
ValidateCommandText(procedure, CommandType.StoredProcedure);
var connectionString = BuildConnectionString(serverOrInstance, database, integratedSecurity, username, password);
return ExecuteStoredProcedure(connectionString, procedure, parameters, useTransaction);
}
/// <summary>
/// Executes a stored procedure using a full SQL Server connection string and existing <see cref="DbParameter"/> instances.
/// </summary>
public virtual object? ExecuteStoredProcedure(
string connectionString,
string procedure,
IEnumerable<DbParameter>? parameters,
bool useTransaction = false)
{
ValidateConnectionString(connectionString);
ValidateCommandText(procedure, CommandType.StoredProcedure);
SqlConnection? connection = null;
SqlTransaction? transaction = null;
var dispose = false;
try
{
(connection, transaction, dispose) = ResolveConnection(connectionString, useTransaction);
using var command = connection.CreateCommand();
command.CommandText = procedure;
command.CommandType = CommandType.StoredProcedure;
command.Transaction = transaction;
AddParameters(command, parameters);
var commandTimeout = CommandTimeout;
if (commandTimeout > 0)
{
command.CommandTimeout = commandTimeout;
}
var dataSet = new DataSet();
using var reader = command.ExecuteReader(CommandBehavior.SequentialAccess);
var tableIndex = 0;
do
{
var table = ReadDataTable(reader, $"Table{tableIndex}");
dataSet.Tables.Add(table);
tableIndex++;
}
while (!reader.IsClosed && reader.NextResult());
return BuildResult(dataSet);
}
catch (SqlException ex)
{
throw new DbaQueryExecutionException("Failed to execute stored procedure.", procedure, ex);
}
catch (InvalidOperationException ex)
{
throw new DbaQueryExecutionException("Failed to execute stored procedure.", procedure, ex);
}
finally
{
if (dispose)
{
DisposeConnection(connection!);
}
}
}
/// <summary>
/// Asynchronously executes a stored procedure using an existing collection of <see cref="DbParameter"/> instances.
/// </summary>
public virtual async Task<object?> ExecuteStoredProcedureAsync(
string serverOrInstance,
string database,
bool integratedSecurity,
string procedure,
IEnumerable<DbParameter>? parameters = null,
bool useTransaction = false,
CancellationToken cancellationToken = default,
string? username = null,
string? password = null)
{
ValidateCommandText(procedure, CommandType.StoredProcedure);
var connectionString = BuildConnectionString(serverOrInstance, database, integratedSecurity, username, password);
return await ExecuteStoredProcedureAsync(connectionString, procedure, parameters, useTransaction, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously executes a stored procedure using a full SQL Server connection string and existing <see cref="DbParameter"/> instances.
/// </summary>
public virtual async Task<object?> ExecuteStoredProcedureAsync(
string connectionString,
string procedure,
IEnumerable<DbParameter>? parameters,
bool useTransaction = false,
CancellationToken cancellationToken = default)
{
ValidateConnectionString(connectionString);
ValidateCommandText(procedure, CommandType.StoredProcedure);
SqlConnection? connection = null;
SqlTransaction? transaction = null;
var dispose = false;
try
{
(connection, transaction, dispose) = await ResolveConnectionAsync(connectionString, useTransaction, cancellationToken).ConfigureAwait(false);
using var command = connection.CreateCommand();
command.CommandText = procedure;
command.CommandType = CommandType.StoredProcedure;
command.Transaction = transaction;
AddParameters(command, parameters);
var commandTimeout = CommandTimeout;
if (commandTimeout > 0)
{
command.CommandTimeout = commandTimeout;
}
var dataSet = new DataSet();
using var reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken).ConfigureAwait(false);
var tableIndex = 0;
do
{
var table = await ReadDataTableAsync(reader, $"Table{tableIndex}", cancellationToken).ConfigureAwait(false);
dataSet.Tables.Add(table);
tableIndex++;
}
while (!reader.IsClosed && await reader.NextResultAsync(cancellationToken).ConfigureAwait(false));
return BuildResult(dataSet);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (SqlException ex)
{
throw new DbaQueryExecutionException("Failed to execute stored procedure.", procedure, ex);
}
catch (InvalidOperationException ex)
{
throw new DbaQueryExecutionException("Failed to execute stored procedure.", procedure, ex);
}
finally
{
await DisposeOwnedResourceAsync(connection, dispose, DisposeConnectionAsync).ConfigureAwait(false);
}
}
}