@@ -54,10 +54,10 @@ public class AdapterTest
5454 public AdapterTest ( )
5555 {
5656 // create random name for temp tables
57- _randomGuid = Guid . NewGuid ( ) . ToString ( ) ;
58- _tempTable = Environment . MachineName + "_" + _randomGuid ;
57+ _tempTable = DataTestUtility . GetUniqueName ( "AdapterTest" ) ;
5958 _tempTable = _tempTable . Replace ( '-' , '_' ) ;
6059
60+ _randomGuid = Guid . NewGuid ( ) . ToString ( ) ;
6161 _tempKey = "employee_id_key_" + Environment . TickCount . ToString ( ) + _randomGuid ;
6262 _tempKey = _tempKey . Replace ( '-' , '_' ) ;
6363
@@ -179,15 +179,15 @@ public void PrepUnprepTest()
179179 public void SqlVariantTest ( )
180180 {
181181 string tableName = DataTestUtility . GenerateObjectName ( ) ;
182- try
182+ // good test for null values and unicode strings
183+ using ( SqlConnection conn = new SqlConnection ( DataTestUtility . TCPConnectionString ) )
184+ using ( SqlCommand cmd = new SqlCommand ( null , conn ) )
185+ using ( SqlDataAdapter sqlAdapter = new SqlDataAdapter ( ) )
183186 {
184- ExecuteNonQueryCommand ( "CREATE TABLE " + tableName + " (c0_bigint bigint, c1_variant sql_variant)" ) ;
185-
186- // good test for null values and unicode strings
187- using ( SqlConnection conn = new SqlConnection ( DataTestUtility . TCPConnectionString ) )
188- using ( SqlCommand cmd = new SqlCommand ( null , conn ) )
189- using ( SqlDataAdapter sqlAdapter = new SqlDataAdapter ( ) )
187+ try
190188 {
189+ ExecuteNonQueryCommand ( "CREATE TABLE " + tableName + " (c0_bigint bigint, c1_variant sql_variant)" ) ;
190+
191191 cmd . Connection . Open ( ) ;
192192
193193 // the ORDER BY clause tests that we correctly ignore the ORDER token
@@ -263,10 +263,10 @@ public void SqlVariantTest()
263263 }
264264 }
265265 }
266- }
267- finally
268- {
269- ExecuteNonQueryCommand ( "DROP TABLE " + tableName ) ;
266+ finally
267+ {
268+ DataTestUtility . DropTable ( conn , tableName ) ;
269+ }
270270 }
271271 }
272272
@@ -659,7 +659,7 @@ public void UpdateTest()
659659 }
660660 finally
661661 {
662- ExecuteNonQueryCommand ( "DROP TABLE " + _tempTable ) ;
662+ DataTestUtility . DropTable ( conn , _tempTable ) ;
663663 }
664664 }
665665 }
@@ -757,7 +757,7 @@ public void BulkUpdateTest()
757757 }
758758 finally
759759 {
760- ExecuteNonQueryCommand ( "DROP TABLE " + _tempTable ) ;
760+ DataTestUtility . DropTable ( conn , _tempTable ) ;
761761 }
762762 }
763763 }
@@ -768,33 +768,35 @@ public void BulkUpdateTest()
768768 [ ConditionalFact ( typeof ( DataTestUtility ) , nameof ( DataTestUtility . AreConnStringsSetup ) , nameof ( DataTestUtility . IsNotAzureSynapse ) ) ]
769769 public void UpdateRefreshTest ( )
770770 {
771+ string identTableName = DataTestUtility . GetUniqueName ( "ID_" ) ;
771772 string createIdentTable =
772- "CREATE TABLE ID_" + _tempTable + " (id int IDENTITY," +
773+ $ "CREATE TABLE { identTableName } (id int IDENTITY," +
773774 "LastName nvarchar(50) NULL," +
774775 "Firstname nvarchar(50) NULL)" ;
775776
777+ string spName = DataTestUtility . GetUniqueName ( "sp_insert" , withBracket : false ) ;
776778 string spCreateInsert =
777- "CREATE PROCEDURE sp_insert" + _tempTable +
779+ $ "CREATE PROCEDURE { spName } " +
778780 "(@FirstName nvarchar(50), @LastName nvarchar(50), @id int OUTPUT) " +
779781 "AS INSERT INTO " + _tempTable + " (FirstName, LastName) " +
780782 "VALUES (@FirstName, @LastName); " +
781783 "SELECT @id=@@IDENTITY" ;
782784
783- string spDropInsert = "DROP PROCEDURE sp_insert" + _tempTable ;
785+ string spDropInsert = $ "DROP PROCEDURE { spName } " ;
784786 bool dropSP = false ;
785787
786788 using ( SqlDataAdapter adapter = new SqlDataAdapter ( ) )
787789 using ( SqlConnection conn = new SqlConnection ( DataTestUtility . TCPConnectionString ) )
788790 using ( SqlCommand cmd = new SqlCommand ( null , conn ) )
789- using ( SqlCommand temp = new SqlCommand ( "SELECT id, LastName, FirstName into " + _tempTable + " from ID_" + _tempTable , conn ) )
791+ using ( SqlCommand temp = new SqlCommand ( "SELECT id, LastName, FirstName into " + _tempTable + $ " from { identTableName } " , conn ) )
790792 using ( SqlCommand tableClean = new SqlCommand ( "" , conn ) )
791793 {
792794 ExecuteNonQueryCommand ( createIdentTable ) ;
793795 try
794796 {
795797 adapter . InsertCommand = new SqlCommand ( )
796798 {
797- CommandText = "sp_insert" + _tempTable ,
799+ CommandText = spName ,
798800 CommandType = CommandType . StoredProcedure
799801 } ;
800802 adapter . InsertCommand . Parameters . Add ( new SqlParameter ( "@FirstName" , SqlDbType . NVarChar , 50 , "FirstName" ) ) ;
@@ -851,9 +853,9 @@ public void UpdateRefreshTest()
851853 {
852854 if ( dropSP )
853855 {
854- ExecuteNonQueryCommand ( spDropInsert ) ;
855- ExecuteNonQueryCommand ( "DROP TABLE " + _tempTable ) ;
856- ExecuteNonQueryCommand ( "DROP TABLE ID_" + _tempTable ) ;
856+ DataTestUtility . DropStoredProcedure ( conn , spName ) ;
857+ DataTestUtility . DropTable ( conn , _tempTable ) ;
858+ DataTestUtility . DropTable ( conn , identTableName ) ;
857859 }
858860 }
859861 }
@@ -873,18 +875,18 @@ public void UpdateNullTest()
873875 "VALUES (@val_cvarbin, @val_cimage)" ;
874876 bool dropSP = false ;
875877
876- try
877- {
878- ExecuteNonQueryCommand ( createTable ) ;
879- ExecuteNonQueryCommand ( createSP ) ;
880- dropSP = true ;
881878
882- using ( SqlConnection conn = new SqlConnection ( DataTestUtility . TCPConnectionString ) )
883- using ( SqlCommand cmdInsert = new SqlCommand ( procName , conn ) )
884- using ( SqlCommand cmdSelect = new SqlCommand ( "select * from " + tableName , conn ) )
885- using ( SqlCommand tableClean = new SqlCommand ( "delete " + tableName , conn ) )
886- using ( SqlDataAdapter adapter = new SqlDataAdapter ( ) )
879+ using ( SqlConnection conn = new SqlConnection ( DataTestUtility . TCPConnectionString ) )
880+ using ( SqlCommand cmdInsert = new SqlCommand ( procName , conn ) )
881+ using ( SqlCommand cmdSelect = new SqlCommand ( "select * from " + tableName , conn ) )
882+ using ( SqlCommand tableClean = new SqlCommand ( "delete " + tableName , conn ) )
883+ using ( SqlDataAdapter adapter = new SqlDataAdapter ( ) )
884+ {
885+ try
887886 {
887+ ExecuteNonQueryCommand ( createTable ) ;
888+ ExecuteNonQueryCommand ( createSP ) ;
889+ dropSP = true ;
888890 conn . Open ( ) ;
889891
890892 cmdInsert . CommandType = CommandType . StoredProcedure ;
@@ -905,13 +907,13 @@ public void UpdateNullTest()
905907 DataTestUtility . AssertEqualsWithDescription ( DBNull . Value , ds . Tables [ 0 ] . Rows [ 0 ] [ 0 ] , "Unexpected value." ) ;
906908 DataTestUtility . AssertEqualsWithDescription ( DBNull . Value , ds . Tables [ 0 ] . Rows [ 0 ] [ 1 ] , "Unexpected value." ) ;
907909 }
908- }
909- finally
910- {
911- if ( dropSP )
910+ finally
912911 {
913- ExecuteNonQueryCommand ( "DROP PROCEDURE " + procName ) ;
914- ExecuteNonQueryCommand ( "DROP TABLE " + tableName ) ;
912+ if ( dropSP )
913+ {
914+ DataTestUtility . DropStoredProcedure ( conn , procName ) ;
915+ DataTestUtility . DropTable ( conn , tableName ) ;
916+ }
915917 }
916918 }
917919 }
@@ -930,18 +932,18 @@ public void UpdateOffsetTest()
930932 "VALUES (@val_cvarbin, @val_cimage)" ;
931933 bool dropSP = false ;
932934
933- try
934- {
935- ExecuteNonQueryCommand ( createTable ) ;
936- ExecuteNonQueryCommand ( createSP ) ;
937- dropSP = true ;
938935
939- using ( SqlConnection conn = new SqlConnection ( DataTestUtility . TCPConnectionString ) )
940- using ( SqlCommand cmdInsert = new SqlCommand ( procName , conn ) )
941- using ( SqlCommand cmdSelect = new SqlCommand ( "select * from " + tableName , conn ) )
942- using ( SqlCommand tableClean = new SqlCommand ( "delete " + tableName , conn ) )
943- using ( SqlDataAdapter adapter = new SqlDataAdapter ( ) )
936+ using ( SqlConnection conn = new SqlConnection ( DataTestUtility . TCPConnectionString ) )
937+ using ( SqlCommand cmdInsert = new SqlCommand ( procName , conn ) )
938+ using ( SqlCommand cmdSelect = new SqlCommand ( "select * from " + tableName , conn ) )
939+ using ( SqlCommand tableClean = new SqlCommand ( "delete " + tableName , conn ) )
940+ using ( SqlDataAdapter adapter = new SqlDataAdapter ( ) )
941+ {
942+ try
944943 {
944+ ExecuteNonQueryCommand ( createTable ) ;
945+ ExecuteNonQueryCommand ( createSP ) ;
946+ dropSP = true ;
945947 conn . Open ( ) ;
946948
947949 cmdInsert . CommandType = CommandType . StoredProcedure ;
@@ -978,13 +980,13 @@ public void UpdateOffsetTest()
978980 val = ( byte [ ] ) ( ds . Tables [ 0 ] . Rows [ 0 ] [ 1 ] ) ;
979981 Assert . True ( ByteArraysEqual ( expectedBytes2 , val ) , "FAILED: Test 2: Unequal byte arrays." ) ;
980982 }
981- }
982- finally
983- {
984- if ( dropSP )
983+ finally
985984 {
986- ExecuteNonQueryCommand ( "DROP PROCEDURE " + procName ) ;
987- ExecuteNonQueryCommand ( "DROP TABLE " + tableName ) ;
985+ if ( dropSP )
986+ {
987+ DataTestUtility . DropStoredProcedure ( conn , procName ) ;
988+ DataTestUtility . DropTable ( conn , tableName ) ;
989+ }
988990 }
989991 }
990992 }
@@ -1069,7 +1071,7 @@ public void AutoGenUpdateTest()
10691071 }
10701072 finally
10711073 {
1072- ExecuteNonQueryCommand ( "DROP TABLE " + _tempTable ) ;
1074+ DataTestUtility . DropTable ( conn , _tempTable ) ;
10731075 }
10741076 }
10751077 }
@@ -1078,16 +1080,17 @@ public void AutoGenUpdateTest()
10781080 [ ConditionalFact ( typeof ( DataTestUtility ) , nameof ( DataTestUtility . AreConnStringsSetup ) , nameof ( DataTestUtility . IsNotAzureSynapse ) ) ]
10791081 public void AutoGenErrorTest ( )
10801082 {
1083+ string identTableName = DataTestUtility . GetUniqueName ( "ID_" ) ;
10811084 string createIdentTable =
1082- "CREATE TABLE ID_" + _tempTable + " (id int IDENTITY," +
1085+ $ "CREATE TABLE { identTableName } (id int IDENTITY," +
10831086 "LastName nvarchar(50) NULL," +
10841087 "Firstname nvarchar(50) NULL)" ;
10851088
1086- try
1089+ using ( SqlConnection conn = new SqlConnection ( DataTestUtility . TCPConnectionString ) )
1090+ using ( SqlCommand cmd = new SqlCommand ( $ "SELECT * into { _tempTable } from { identTableName } ", conn ) )
1091+ using ( SqlDataAdapter adapter = new SqlDataAdapter ( ) )
10871092 {
1088- using ( SqlConnection conn = new SqlConnection ( DataTestUtility . TCPConnectionString ) )
1089- using ( SqlCommand cmd = new SqlCommand ( "SELECT * into " + _tempTable + " from ID_" + _tempTable , conn ) )
1090- using ( SqlDataAdapter adapter = new SqlDataAdapter ( ) )
1093+ try
10911094 {
10921095 ExecuteNonQueryCommand ( createIdentTable ) ;
10931096
@@ -1110,11 +1113,11 @@ public void AutoGenErrorTest()
11101113 SqlCommandBuilder builder = new SqlCommandBuilder ( adapter ) ;
11111114 adapter . Update ( ds , _tempTable ) ;
11121115 }
1113- }
1114- finally
1115- {
1116- ExecuteNonQueryCommand ( "DROP TABLE " + _tempTable ) ;
1117- ExecuteNonQueryCommand ( "DROP TABLE ID_" + _tempTable ) ;
1116+ finally
1117+ {
1118+ DataTestUtility . DropTable ( conn , _tempTable ) ;
1119+ DataTestUtility . DropTable ( conn , identTableName ) ;
1120+ }
11181121 }
11191122 }
11201123
@@ -1206,7 +1209,7 @@ public void AutoGenBulkUpdateTest()
12061209 }
12071210 finally
12081211 {
1209- ExecuteNonQueryCommand ( "DROP TABLE " + _tempTable ) ;
1212+ DataTestUtility . DropTable ( conn , _tempTable ) ;
12101213 }
12111214 }
12121215 }
@@ -1319,6 +1322,48 @@ public void TestReadOnlyColumnMetadata()
13191322 }
13201323 }
13211324
1325+ [ ConditionalTheory ( typeof ( DataTestUtility ) , nameof ( DataTestUtility . AreConnStringsSetup ) , nameof ( DataTestUtility . IsNotAzureSynapse ) ) ]
1326+ [ InlineData ( nameof ( SqlCommandBuilder . GetInsertCommand ) , null ) ]
1327+ [ InlineData ( nameof ( SqlCommandBuilder . GetInsertCommand ) , true ) ]
1328+ [ InlineData ( nameof ( SqlCommandBuilder . GetInsertCommand ) , false ) ]
1329+ [ InlineData ( nameof ( SqlCommandBuilder . GetUpdateCommand ) , null ) ]
1330+ [ InlineData ( nameof ( SqlCommandBuilder . GetUpdateCommand ) , true ) ]
1331+ [ InlineData ( nameof ( SqlCommandBuilder . GetUpdateCommand ) , false ) ]
1332+ [ InlineData ( nameof ( SqlCommandBuilder . GetDeleteCommand ) , null ) ]
1333+ [ InlineData ( nameof ( SqlCommandBuilder . GetDeleteCommand ) , false ) ]
1334+ [ InlineData ( nameof ( SqlCommandBuilder . GetDeleteCommand ) , true ) ]
1335+ public void VerifyGetCommand ( string methodName , bool ? useColumnsForParameterNames )
1336+ {
1337+ using ( SqlConnection connection = new SqlConnection ( DataTestUtility . TCPConnectionString ) )
1338+ {
1339+ connection . Open ( ) ;
1340+ using ( SqlDataAdapter dataAdapter = new SqlDataAdapter ( "SELECT * FROM dbo.Customers" , connection ) )
1341+ {
1342+ using ( SqlCommandBuilder commandBuilder = new SqlCommandBuilder ( dataAdapter ) )
1343+ {
1344+ object [ ] parameters = null ;
1345+ Type [ ] parameterTypes = null ;
1346+ if ( useColumnsForParameterNames != null )
1347+ {
1348+ parameters = new object [ ] { useColumnsForParameterNames } ;
1349+ parameterTypes = new Type [ ] { typeof ( bool ) } ;
1350+ }
1351+ else
1352+ {
1353+ parameters = new object [ ] { } ;
1354+ parameterTypes = new Type [ ] { } ;
1355+ }
1356+
1357+ MethodInfo method = commandBuilder . GetType ( ) . GetMethod ( methodName , parameterTypes ) ;
1358+ using ( SqlCommand cmd = ( SqlCommand ) method . Invoke ( commandBuilder , parameters ) )
1359+ {
1360+ Assert . NotNull ( cmd ) ;
1361+ }
1362+ }
1363+ }
1364+ }
1365+ }
1366+
13221367 #region Utility_Methods
13231368 private void CheckParameters ( SqlCommand cmd , string expectedResults )
13241369 {
0 commit comments