11// Copyright (c) Microsoft Corporation. All rights reserved.
22
33using System . ComponentModel ;
4- using System . Data ;
54using Microsoft . Data . SqlClient ;
6- using ModelContextProtocol . Server ;
75using Microsoft . Extensions . Logging ;
6+ using ModelContextProtocol . Server ;
87
98namespace Mssql . McpServer ;
109public partial class Tools
@@ -20,15 +19,23 @@ public async Task<DbOperationResult> ReadData(
2019 {
2120 using var cmd = new SqlCommand ( sql , conn ) ;
2221 using var reader = await cmd . ExecuteReaderAsync ( ) ;
23- var table = new DataTable ( ) ;
24- table . Load ( reader ) ;
25- return new DbOperationResult { Success = true , Data = DataTableToList ( table ) } ;
22+ var results = new List < Dictionary < string , object ? > > ( ) ;
23+ while ( await reader . ReadAsync ( ) )
24+ {
25+ var row = new Dictionary < string , object ? > ( ) ;
26+ for ( int i = 0 ; i < reader . FieldCount ; i ++ )
27+ {
28+ row [ reader . GetName ( i ) ] = reader . IsDBNull ( i ) ? null : reader . GetValue ( i ) ;
29+ }
30+ results . Add ( row ) ;
31+ }
32+ return new DbOperationResult ( success : true , data : results ) ;
2633 }
2734 }
2835 catch ( Exception ex )
2936 {
3037 _logger . LogError ( ex , "ReadData failed: {Message}" , ex . Message ) ;
31- return new DbOperationResult { Success = false , Error = ex . Message } ;
38+ return new DbOperationResult ( success : false , error : ex . Message ) ;
3239 }
3340 }
3441}
0 commit comments