@@ -117,21 +117,25 @@ public List<List<string>> GetValues(string schema, string table, params string[]
117
117
{
118
118
List < List < string > > rows = new List < List < string > > ( ) ;
119
119
this . OpenConnection ( ) ;
120
- SqlCommand command = this . connection . CreateCommand ( ) ;
121
- command . CommandText = string . Format ( Resources . ReadValuesCommand , string . Join ( ", " , columns ) , schema , table ) ;
122
- command . Parameters . AddWithValue ( "schema" , schema ) ;
123
- command . Parameters . AddWithValue ( "name" , table ) ;
124
- SqlDataReader reader = command . ExecuteReader ( ) ;
125
- if ( reader . HasRows )
120
+ using ( SqlCommand command = this . connection . CreateCommand ( ) )
126
121
{
127
- while ( reader . Read ( ) )
122
+ command . CommandText = string . Format ( Resources . ReadValuesCommand , string . Join ( ", " , columns ) , schema , table ) ;
123
+ command . Parameters . AddWithValue ( "schema" , schema ) ;
124
+ command . Parameters . AddWithValue ( "name" , table ) ;
125
+ using ( SqlDataReader reader = command . ExecuteReader ( ) )
128
126
{
129
- List < string > row = new List < string > ( ) ;
130
- for ( int ordinal = 0 ; ordinal < columns . Length ; ordinal ++ )
127
+ if ( reader . HasRows )
131
128
{
132
- row . Add ( reader . GetValue ( ordinal ) . ToString ( ) ) ;
129
+ while ( reader . Read ( ) )
130
+ {
131
+ List < string > row = new List < string > ( ) ;
132
+ for ( int ordinal = 0 ; ordinal < columns . Length ; ordinal ++ )
133
+ {
134
+ row . Add ( reader . GetValue ( ordinal ) . ToString ( ) ) ;
135
+ }
136
+ rows . Add ( row ) ;
137
+ }
133
138
}
134
- rows . Add ( row ) ;
135
139
}
136
140
}
137
141
return rows ;
@@ -144,22 +148,24 @@ public List<TsqlParameter> GetParameters(string schema, string storedProcedure)
144
148
try
145
149
{
146
150
this . OpenConnection ( ) ;
147
- SqlCommand command = this . connection . CreateCommand ( ) ;
148
- command . CommandText = Resources . ReadParametersCommand ;
149
- command . Parameters . AddWithValue ( "schema" , schema ) ;
150
- command . Parameters . AddWithValue ( "name" , storedProcedure ) ;
151
- using ( SqlDataReader reader = command . ExecuteReader ( ) )
151
+ using ( SqlCommand command = this . connection . CreateCommand ( ) )
152
152
{
153
- if ( reader . HasRows )
153
+ command . CommandText = Resources . ReadParametersCommand ;
154
+ command . Parameters . AddWithValue ( "schema" , schema ) ;
155
+ command . Parameters . AddWithValue ( "name" , storedProcedure ) ;
156
+ using ( SqlDataReader reader = command . ExecuteReader ( ) )
154
157
{
155
- while ( reader . Read ( ) )
158
+ if ( reader . HasRows )
156
159
{
157
- TsqlParameter parameter = new TsqlParameter ( ) ;
158
- parameter . Name = reader . GetString ( reader . GetOrdinal ( "PARAMETER_NAME" ) ) ;
159
- parameter . Type = reader . GetString ( reader . GetOrdinal ( "DATA_TYPE" ) ) ;
160
- parameter . Order = reader . GetInt32 ( reader . GetOrdinal ( "ORDINAL_POSITION" ) ) ;
161
- parameter . IsNullable = true ;
162
- list . Add ( parameter ) ;
160
+ while ( reader . Read ( ) )
161
+ {
162
+ TsqlParameter parameter = new TsqlParameter ( ) ;
163
+ parameter . Name = reader . GetString ( reader . GetOrdinal ( "PARAMETER_NAME" ) ) ;
164
+ parameter . Type = reader . GetString ( reader . GetOrdinal ( "DATA_TYPE" ) ) ;
165
+ parameter . Order = reader . GetInt32 ( reader . GetOrdinal ( "ORDINAL_POSITION" ) ) ;
166
+ parameter . IsNullable = true ;
167
+ list . Add ( parameter ) ;
168
+ }
163
169
}
164
170
}
165
171
}
@@ -182,27 +188,33 @@ public List<TsqlColumn> GetColumnsFromStoredProcedure(string schema, string stor
182
188
this . LastError = null ;
183
189
string key = $ "{ schema } .{ storedProcedure } ";
184
190
if ( this . columnsCache . ContainsKey ( key ) )
191
+ {
185
192
return this . columnsCache [ key ] ;
193
+ }
186
194
187
195
List < TsqlColumn > columns = new List < TsqlColumn > ( ) ;
188
196
try
189
197
{
190
198
this . OpenConnection ( ) ;
191
- SqlCommand command = this . connection . CreateCommand ( ) ;
192
- command . CommandText = string . Format ( Resources . ReadResultCommand , key ) ;
193
- SqlDataReader reader = command . ExecuteReader ( ) ;
194
- if ( reader . HasRows )
199
+ using ( SqlCommand command = this . connection . CreateCommand ( ) )
195
200
{
196
- while ( reader . Read ( ) )
201
+ command . CommandText = string . Format ( Resources . ReadResultCommand , key ) ;
202
+ using ( SqlDataReader reader = command . ExecuteReader ( ) )
197
203
{
198
- columns . Add ( this . ReadColumn ( reader ) ) ;
204
+ if ( reader . HasRows )
205
+ {
206
+ while ( reader . Read ( ) )
207
+ {
208
+ columns . Add ( this . ReadColumn ( reader ) ) ;
209
+ }
210
+ }
211
+ this . columnsCache . Add ( key , columns ) ;
212
+ if ( columns . All ( x => ! x . IsPrimaryKey ) )
213
+ {
214
+ columns . ForEach ( x => x . IsPrimaryKey = ! x . IsNullable ) ;
215
+ }
199
216
}
200
217
}
201
- this . columnsCache . Add ( key , columns ) ;
202
- if ( columns . All ( x => ! x . IsPrimaryKey ) )
203
- {
204
- columns . ForEach ( x => x . IsPrimaryKey = ! x . IsNullable ) ;
205
- }
206
218
}
207
219
catch ( SqlException exception )
208
220
{
0 commit comments