private static void OpenBadConnection(string connectionString, string errorMessage) { using (SqlConnection conn = new SqlConnection(connectionString)) { DataTestClass.AssertThrowsWrapper <SqlException>(() => conn.Open(), errorMessage); } }
private void MultipleExecutesInSameTransactionTest_shouldThrowsUnsupported(string connectionString) { string expectedErrorMessage = SystemDataResourceManager.Instance.ADP_ParallelTransactionsNotSupported(typeof(SqlConnection).Name); string tempTableName = ""; try { tempTableName = CreateTempTable(connectionString); DataTestClass.AssertThrowsWrapper <InvalidOperationException>( actionThatFails: () => { MultipleExecutesInSameTransactionTest(connectionString, tempTableName); }, exceptionMessage: expectedErrorMessage); } finally { if (!string.IsNullOrEmpty(tempTableName)) { DropTempTable(connectionString, tempTableName); } } }
public static void CodeCoverageSqlClient() { SqlParameterCollection opc = new SqlCommand().Parameters; Assert.True(opc.Count == 0, string.Format("FAILED: Expected count: {0}. Actual count: {1}.", 0, opc.Count)); Assert.False(((IList)opc).IsReadOnly, "FAILED: Expected collection to NOT be read only."); Assert.False(((IList)opc).IsFixedSize, "FAILED: Expected collection to NOT be fixed size."); Assert.False(((IList)opc).IsSynchronized, "FAILED: Expected collection to NOT be synchronized."); DataTestClass.AssertEqualsWithDescription("Object", ((IList)opc).SyncRoot.GetType().Name, "FAILED: Incorrect SyncRoot Name"); { string failValue; DataTestClass.AssertThrowsWrapper <IndexOutOfRangeException>(() => failValue = opc[0].ParameterName, "Invalid index 0 for this SqlParameterCollection with Count=0."); DataTestClass.AssertThrowsWrapper <IndexOutOfRangeException>(() => failValue = opc["@p1"].ParameterName, "An SqlParameter with ParameterName '@p1' is not contained by this SqlParameterCollection."); } DataTestClass.AssertThrowsWrapper <ArgumentNullException>(() => opc.Add(null), "The SqlParameterCollection only accepts non-null SqlParameter type objects."); opc.Add((object)new SqlParameter()); IEnumerator enm = opc.GetEnumerator(); Assert.True(enm.MoveNext(), "FAILED: Expected MoveNext to be true"); DataTestClass.AssertEqualsWithDescription("Parameter1", ((SqlParameter)enm.Current).ParameterName, "FAILED: Incorrect ParameterName"); opc.Add(new SqlParameter()); DataTestClass.AssertEqualsWithDescription("Parameter2", opc[1].ParameterName, "FAILED: Incorrect ParameterName"); opc.Add(new SqlParameter(null, null)); opc.Add(new SqlParameter(null, SqlDbType.Int)); DataTestClass.AssertEqualsWithDescription("Parameter4", opc["Parameter4"].ParameterName, "FAILED: Incorrect ParameterName"); opc.Add(new SqlParameter("Parameter5", SqlDbType.NVarChar, 20)); opc.Add(new SqlParameter(null, SqlDbType.NVarChar, 20, "a")); opc.RemoveAt(opc[3].ParameterName); DataTestClass.AssertEqualsWithDescription(-1, opc.IndexOf(null), "FAILED: Incorrect index for null value"); SqlParameter p = opc[0]; DataTestClass.AssertThrowsWrapper <ArgumentException>(() => opc.Add((object)p), "The SqlParameter is already contained by another SqlParameterCollection."); DataTestClass.AssertThrowsWrapper <ArgumentException>(() => new SqlCommand().Parameters.Add(p), "The SqlParameter is already contained by another SqlParameterCollection."); DataTestClass.AssertThrowsWrapper <ArgumentNullException>(() => opc.Remove(null), "The SqlParameterCollection only accepts non-null SqlParameter type objects."); string pname = p.ParameterName; p.ParameterName = pname; p.ParameterName = pname.ToUpper(); p.ParameterName = pname.ToLower(); p.ParameterName = "@p1"; p.ParameterName = pname; opc.Clear(); opc.Add(p); opc.Clear(); opc.AddWithValue("@p1", null); DataTestClass.AssertEqualsWithDescription(-1, opc.IndexOf(p.ParameterName), "FAILED: Incorrect index for parameter name"); opc[0] = p; DataTestClass.AssertEqualsWithDescription(0, opc.IndexOf(p.ParameterName), "FAILED: Incorrect index for parameter name"); Assert.True(opc.Contains(p.ParameterName), "FAILED: Expected collection to contain provided parameter."); Assert.True(opc.Contains(opc[0]), "FAILED: Expected collection to contain provided parameter."); opc[0] = p; opc[p.ParameterName] = new SqlParameter(p.ParameterName, null); opc[p.ParameterName] = new SqlParameter(); opc.RemoveAt(0); new SqlCommand().Parameters.Clear(); new SqlCommand().Parameters.CopyTo(new object[0], 0); Assert.False(new SqlCommand().Parameters.GetEnumerator().MoveNext(), "FAILED: Expected MoveNext to be false"); DataTestClass.AssertThrowsWrapper <InvalidCastException>(() => new SqlCommand().Parameters.Add(0), "The SqlParameterCollection only accepts non-null SqlParameter type objects, not Int32 objects."); DataTestClass.AssertThrowsWrapper <InvalidCastException>(() => new SqlCommand().Parameters.Insert(0, 0), "The SqlParameterCollection only accepts non-null SqlParameter type objects, not Int32 objects."); DataTestClass.AssertThrowsWrapper <InvalidCastException>(() => new SqlCommand().Parameters.Remove(0), "The SqlParameterCollection only accepts non-null SqlParameter type objects, not Int32 objects."); DataTestClass.AssertThrowsWrapper <ArgumentException>(() => new SqlCommand().Parameters.Remove(new SqlParameter()), "Attempted to remove an SqlParameter that is not contained by this SqlParameterCollection."); }