/
SqlConnection.ExecuteReader.cs
140 lines (128 loc) · 6.01 KB
/
SqlConnection.ExecuteReader.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
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
// Description: C# Extension Methods | Enhance the .NET Framework and .NET Core with over 1000 extension methods.
// Website & Documentation: https://csharp-extension.com/
// Issues: https://github.com/zzzprojects/Z.ExtensionMethods/issues
// License (MIT): https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE
// More projects: https://zzzprojects.com/
// Copyright © ZZZ Projects Inc. All rights reserved.
using System;
using System.Data;
using System.Data.SqlClient;
public static partial class Extensions
{
/// <summary>
/// A SqlConnection extension method that executes the reader operation.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="cmdText">The command text.</param>
/// <param name="parameters">Options for controlling the operation.</param>
/// <param name="commandType">Type of the command.</param>
/// <param name="transaction">The transaction.</param>
/// <returns>A SqlDataReader.</returns>
public static SqlDataReader ExecuteReader(this SqlConnection @this, string cmdText, SqlParameter[] parameters, CommandType commandType, SqlTransaction transaction)
{
using (SqlCommand command = @this.CreateCommand())
{
command.CommandText = cmdText;
command.CommandType = commandType;
command.Transaction = transaction;
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
return command.ExecuteReader();
}
}
/// <summary>
/// A SqlConnection extension method that executes the reader operation.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="commandFactory">The command factory.</param>
/// <returns>A SqlDataReader.</returns>
public static SqlDataReader ExecuteReader(this SqlConnection @this, Action<SqlCommand> commandFactory)
{
using (SqlCommand command = @this.CreateCommand())
{
commandFactory(command);
return command.ExecuteReader();
}
}
/// <summary>
/// A SqlConnection extension method that executes the reader operation.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="cmdText">The command text.</param>
/// <returns>A SqlDataReader.</returns>
public static SqlDataReader ExecuteReader(this SqlConnection @this, string cmdText)
{
return @this.ExecuteReader(cmdText, null, CommandType.Text, null);
}
/// <summary>
/// A SqlConnection extension method that executes the reader operation.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="cmdText">The command text.</param>
/// <param name="transaction">The transaction.</param>
/// <returns>A SqlDataReader.</returns>
public static SqlDataReader ExecuteReader(this SqlConnection @this, string cmdText, SqlTransaction transaction)
{
return @this.ExecuteReader(cmdText, null, CommandType.Text, transaction);
}
/// <summary>
/// A SqlConnection extension method that executes the reader operation.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="cmdText">The command text.</param>
/// <param name="commandType">Type of the command.</param>
/// <returns>A SqlDataReader.</returns>
public static SqlDataReader ExecuteReader(this SqlConnection @this, string cmdText, CommandType commandType)
{
return @this.ExecuteReader(cmdText, null, commandType, null);
}
/// <summary>
/// A SqlConnection extension method that executes the reader operation.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="cmdText">The command text.</param>
/// <param name="commandType">Type of the command.</param>
/// <param name="transaction">The transaction.</param>
/// <returns>A SqlDataReader.</returns>
public static SqlDataReader ExecuteReader(this SqlConnection @this, string cmdText, CommandType commandType, SqlTransaction transaction)
{
return @this.ExecuteReader(cmdText, null, commandType, transaction);
}
/// <summary>
/// A SqlConnection extension method that executes the reader operation.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="cmdText">The command text.</param>
/// <param name="parameters">Options for controlling the operation.</param>
/// <returns>A SqlDataReader.</returns>
public static SqlDataReader ExecuteReader(this SqlConnection @this, string cmdText, SqlParameter[] parameters)
{
return @this.ExecuteReader(cmdText, parameters, CommandType.Text, null);
}
/// <summary>
/// A SqlConnection extension method that executes the reader operation.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="cmdText">The command text.</param>
/// <param name="parameters">Options for controlling the operation.</param>
/// <param name="transaction">The transaction.</param>
/// <returns>A SqlDataReader.</returns>
public static SqlDataReader ExecuteReader(this SqlConnection @this, string cmdText, SqlParameter[] parameters, SqlTransaction transaction)
{
return @this.ExecuteReader(cmdText, parameters, CommandType.Text, transaction);
}
/// <summary>
/// A SqlConnection extension method that executes the reader operation.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="cmdText">The command text.</param>
/// <param name="parameters">Options for controlling the operation.</param>
/// <param name="commandType">Type of the command.</param>
/// <returns>A SqlDataReader.</returns>
public static SqlDataReader ExecuteReader(this SqlConnection @this, string cmdText, SqlParameter[] parameters, CommandType commandType)
{
return @this.ExecuteReader(cmdText, parameters, commandType, null);
}
}