1010using Xunit ;
1111using Xunit . Abstractions ;
1212using System . Text . Json ;
13+ using Microsoft . Data . SqlTypes ;
1314
1415namespace Microsoft . Data . SqlClient . ManualTesting . Tests
1516{
@@ -124,6 +125,12 @@ public void TestJsonWrite()
124125 ValidateRowsAffected ( rowsAffected3 ) ;
125126 }
126127
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+
127134 DataTestUtility . DropTable ( connection , tableName ) ;
128135 DataTestUtility . DropStoredProcedure ( connection , spName ) ;
129136 }
@@ -182,6 +189,12 @@ public async Task TestJsonWriteAsync()
182189 ValidateRowsAffected ( rowsAffected3 ) ;
183190 }
184191
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+
185198 DataTestUtility . DropTable ( connection , tableName ) ;
186199 DataTestUtility . DropStoredProcedure ( connection , spName ) ;
187200 }
@@ -354,7 +367,7 @@ public void TestJsonAPIs()
354367 // Create Table
355368 DataTestUtility . CreateTable ( connection , tableName , "(Data json)" ) ;
356369
357- // Insert Null value
370+ //Insert
358371 command . CommandText = tableInsert ;
359372 var parameter = new SqlParameter ( "@jsonData" , SqlDbTypeExtensions . Json ) ;
360373 parameter . Value = JsonDataString ;
@@ -373,6 +386,7 @@ public void TestJsonAPIs()
373386 Assert . Equal ( JsonDataString , jsonDocument . RootElement . ToString ( ) ) ;
374387 Assert . Equal ( "json" , reader . GetDataTypeName ( 0 ) ) ;
375388 Assert . Equal ( "System.String" , reader . GetFieldType ( 0 ) . ToString ( ) ) ;
389+ Assert . Equal ( JsonDataString , reader . GetSqlJson ( 0 ) . Value ) ;
376390 }
377391 }
378392 }
@@ -439,5 +453,57 @@ public void TestJsonWithMARS()
439453 }
440454 }
441455 }
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+ }
442508 }
443509}
0 commit comments