示例#1
0
        public void RecordExample()
        {
            // To create a record that implement IDataRecord we start with a record set definition.
            RecordSetDefinition recordSetDefinition = new RecordSetDefinition(
                new ColumnDefinition("ID", SqlDbType.Int),
                new ColumnDefinition("Name", SqlDbType.Char, 50),
                new ColumnDefinition("Description", SqlDbType.NVarChar),
                // This column is not nullable so defaults to true
                new ColumnDefinition("Active", SqlDbType.Bit, isNullable: false, defaultValue: true)
                );

            // Now we can create a record
            IObjectRecord dataRecord = new ObjectRecord(recordSetDefinition, 1, "Test", "This is my test record");

            // Or we can create one with random values
            IObjectRecord randomRecord = new ObjectRecord(recordSetDefinition, true);

            // To create a record that throws an exception we first create a SqlException
            // We can't do this directly, but we can use our prototypes to construct one.

            // SqlExceptions are made from a collection of SqlErrors - which can make like this :
            SqlErrorCollection errorCollection = new SqlErrorCollectionPrototype
            {
                new SqlErrorPrototype(
                    1000,
                    80,
                    17,
                    "MyFakeServer",
                    "Connection Timeout.",
                    "spMySproc",
                    54)
            };

            SqlException  sqlException    = new SqlExceptionPrototype(errorCollection, "9.0.0.0", Guid.NewGuid());
            IObjectRecord exceptionRecord = new ExceptionRecord(sqlException);

            // We can stick these records into a recordset
            // Note the records must have the same RecordSetDefinition (unless it's an exception record)
            // The final record will through an exception when reached!
            ObjectSet recordSet = new ObjectSet(recordSetDefinition)
            {
                dataRecord,
                randomRecord,
                //exceptionRecord
            };

            // We can add recordsets to an ObjectReader
            ObjectReader reader = new ObjectReader
            {
                recordSet
            };

            // We can also add random record sets - this one has the same definition as the first.
            reader.Add(new RandomSet(recordSetDefinition));

            // We can also fix certain rows values using the column generators arry, a null indicates
            // that the column should us a random value, otherwise a lambda can be supplied - in this case
            // it sets the row to the row number (1 - indexed).
            reader.Add(
                new RandomSet(
                    recordSetDefinition,
                    columnGenerators: new Func <int, object>[] { null, row => "Row #" + row }));

            // Whereas this one has a random set of columns (with random types).
            reader.Add(new RandomSet(10));

            // Now that we have a reader we can use it like a normal reader - it even simulates disposal.
            using (IDataReader dataReader = reader)
            {
                int recordset = 1;
                do
                {
                    Trace.Write("Recordset #" + recordset);
                    int rows = 0;
                    while (dataReader.Read())
                    {
                        rows++;
                    }
                    Trace.WriteLine(" - " + rows + " rows.");
                    recordset++;
                } while (dataReader.NextResult());
            }
        }
示例#2
0
        public void TestSqlExceptionPrototype()
        {
            Parallel.For(
                0,
                100,
                i =>
            {
                Random random = Tester.RandomGenerator;
                SqlErrorCollectionPrototype errorCollectionPrototype =
                    new SqlErrorCollectionPrototype();

                int loops = Tester.RandomGenerator.Next(10) + 1;
                for (int loop = 0; loop < loops; loop++)
                {
                    // Generate random values.
                    int infoNumber       = random.RandomInt32();
                    byte errorState      = random.RandomByte();
                    byte errorClass      = (byte)random.Next(1, 26);
                    string server        = random.RandomString();
                    string errorMessage  = random.RandomString();
                    string procedure     = random.RandomString();
                    int lineNumber       = random.RandomInt32();
                    uint wind32ErrorCode = (uint)Math.Abs(random.RandomInt32());

                    // Create prototype.
                    SqlErrorPrototype sqlErrorPrototype = new SqlErrorPrototype(
                        infoNumber,
                        errorState,
                        errorClass,
                        server,
                        errorMessage,
                        procedure,
                        lineNumber,
                        wind32ErrorCode);

                    // Test implicit cast
                    SqlError sqlError = sqlErrorPrototype;
                    Assert.IsNotNull(sqlError);

                    // Check SqlError created properly
                    Assert.AreEqual(infoNumber, sqlError.Number);
                    Assert.AreEqual(errorState, sqlError.State);
                    Assert.AreEqual(errorClass, sqlError.Class);
                    Assert.AreEqual(server, sqlError.Server);
                    Assert.AreEqual(errorMessage, sqlError.Message);
                    Assert.AreEqual(procedure, sqlError.Procedure);
                    Assert.AreEqual(lineNumber, sqlError.LineNumber);
                    Assert.AreEqual(sqlErrorPrototype.ToString(), sqlError.ToString());

                    errorCollectionPrototype.Add(sqlError);
                }

                Assert.AreEqual(loops, errorCollectionPrototype.Count);

                // Test implicit cast
                SqlErrorCollection collection = errorCollectionPrototype;

                Assert.AreSame(errorCollectionPrototype.SqlErrorCollection, collection);

                // Now create a SqlException
                Guid connectionId = Guid.NewGuid();
                SqlExceptionPrototype sqlExceptionPrototype = new SqlExceptionPrototype(
                    collection,
                    "9.0.0.0",
                    connectionId);

                // Test implicit conversion
                SqlException sqlException = sqlExceptionPrototype;
                Assert.IsNotNull(sqlException);

                // Check SqlException created properly - it uses the first error from the collection.
                SqlError first = collection[0];
                Debug.Assert(first != null);
                Assert.AreEqual(first.Number, sqlException.Number);
                Assert.AreEqual(first.State, sqlException.State);
                Assert.AreEqual(first.Class, sqlException.Class);
                Assert.AreEqual(first.Server, sqlException.Server);
                //Assert.AreEqual(first.Message, sqlException.Message);
                Assert.AreEqual(first.Procedure, sqlException.Procedure);
                Assert.AreEqual(first.LineNumber, sqlException.LineNumber);
            });
        }