10
10
using Xunit ;
11
11
using Xunit . Abstractions ;
12
12
using System . Text . Json ;
13
+ using Microsoft . Data . SqlTypes ;
13
14
14
15
namespace Microsoft . Data . SqlClient . ManualTesting . Tests
15
16
{
@@ -124,6 +125,12 @@ public void TestJsonWrite()
124
125
ValidateRowsAffected ( rowsAffected3 ) ;
125
126
}
126
127
128
+ //Test 4
129
+ // Write json value using a parameterized query with SqlJson type
130
+ parameter . Value = new SqlJson ( JsonDataString ) ;
131
+ int rowsAffected4 = command . ExecuteNonQuery ( ) ;
132
+ ValidateRowsAffected ( rowsAffected4 ) ;
133
+
127
134
DataTestUtility . DropTable ( connection , tableName ) ;
128
135
DataTestUtility . DropStoredProcedure ( connection , spName ) ;
129
136
}
@@ -182,6 +189,12 @@ public async Task TestJsonWriteAsync()
182
189
ValidateRowsAffected ( rowsAffected3 ) ;
183
190
}
184
191
192
+ //Test 4
193
+ // Write json value using a parameterized query with SqlJson type
194
+ parameter . Value = new SqlJson ( JsonDataString ) ;
195
+ int rowsAffected4 = await command . ExecuteNonQueryAsync ( ) ;
196
+ ValidateRowsAffected ( rowsAffected4 ) ;
197
+
185
198
DataTestUtility . DropTable ( connection , tableName ) ;
186
199
DataTestUtility . DropStoredProcedure ( connection , spName ) ;
187
200
}
@@ -354,7 +367,7 @@ public void TestJsonAPIs()
354
367
// Create Table
355
368
DataTestUtility . CreateTable ( connection , tableName , "(Data json)" ) ;
356
369
357
- // Insert Null value
370
+ //Insert
358
371
command . CommandText = tableInsert ;
359
372
var parameter = new SqlParameter ( "@jsonData" , SqlDbTypeExtensions . Json ) ;
360
373
parameter . Value = JsonDataString ;
@@ -373,6 +386,7 @@ public void TestJsonAPIs()
373
386
Assert . Equal ( JsonDataString , jsonDocument . RootElement . ToString ( ) ) ;
374
387
Assert . Equal ( "json" , reader . GetDataTypeName ( 0 ) ) ;
375
388
Assert . Equal ( "System.String" , reader . GetFieldType ( 0 ) . ToString ( ) ) ;
389
+ Assert . Equal ( JsonDataString , reader . GetSqlJson ( 0 ) . Value ) ;
376
390
}
377
391
}
378
392
}
@@ -439,5 +453,57 @@ public void TestJsonWithMARS()
439
453
}
440
454
}
441
455
}
456
+
457
+ [ ConditionalFact ( typeof ( DataTestUtility ) , nameof ( DataTestUtility . IsJsonSupported ) ) ]
458
+ public void TestJsonSPParams ( )
459
+ {
460
+ string tableName = DataTestUtility . GenerateObjectName ( ) ;
461
+ string procName = DataTestUtility . GenerateObjectName ( ) ;
462
+ string tableInsert = $ "INSERT INTO { tableName } VALUES (@id, @jsonData)";
463
+ string tableRead = $ "SELECT * FROM { tableName } ";
464
+
465
+ using ( SqlConnection connection = new SqlConnection ( DataTestUtility . TCPConnectionString ) )
466
+ {
467
+ connection . Open ( ) ;
468
+ try
469
+ {
470
+ // Create Table
471
+ DataTestUtility . CreateTable ( connection , tableName , "(Id int, Data json)" ) ;
472
+
473
+ // Create Stored Procedure
474
+ string createSP = $@ "
475
+ @id int,
476
+ @jsonData json OUTPUT
477
+ AS
478
+ BEGIN
479
+ SELECT @jsonData = (SELECT Data FROM { tableName } WHERE Id = @id)
480
+ END;" ;
481
+ DataTestUtility . CreateSP ( connection , procName , createSP ) ;
482
+
483
+ // Insert Data
484
+ using ( SqlCommand command = new SqlCommand ( tableInsert , connection ) )
485
+ {
486
+ command . Parameters . Add ( new SqlParameter ( "@id" , SqlDbType . Int ) { Value = 1 } ) ;
487
+ command . Parameters . Add ( new SqlParameter ( "@jsonData" , SqlDbTypeExtensions . Json ) { Value = JsonDataString } ) ;
488
+ command . ExecuteNonQuery ( ) ;
489
+ }
490
+
491
+ // Execute Stored Procedure
492
+ using ( SqlCommand spCommand = new SqlCommand ( procName , connection ) )
493
+ {
494
+ spCommand . CommandType = CommandType . StoredProcedure ;
495
+ spCommand . Parameters . Add ( new SqlParameter ( "@id" , SqlDbType . Int ) { Direction = ParameterDirection . Input , Value = 1 } ) ;
496
+ SqlParameter outputParam = new SqlParameter ( "@jsonData" , SqlDbTypeExtensions . Json ) { Direction = ParameterDirection . Output } ;
497
+ spCommand . Parameters . Add ( outputParam ) ;
498
+ spCommand . ExecuteNonQuery ( ) ;
499
+ Assert . Equal ( JsonDataString , ( string ) outputParam . Value ) ;
500
+ }
501
+ }
502
+ finally
503
+ {
504
+ DataTestUtility . DropTable ( connection , tableName ) ;
505
+ }
506
+ }
507
+ }
442
508
}
443
509
}
0 commit comments