Пример #1
0
        public static void NonrepeatableReadFirstThread(String connStrig, IsolationLevel level)
        {
            Console.WriteLine("Begin the NonrepeatableReadFirstThread.....");

            using (SqlConnection conn = new SqlConnection(connStrig))
            {
                String cmdText = @"Use DbDataIsolationLevel; 

                    Select ProductId,ProductName,Quantity,Price
                    from dbo.Products
                    where ProductId=1

                    WaitFor Delay '00:00:06';

                    Select ProductId,ProductName,Quantity,Price
                    from dbo.Products
                    where ProductId=1";

                conn.Open();

                using (SqlTransaction tran = conn.BeginTransaction(level, "NonrepeatableReadFirst"))
                {
                    using (SqlCommand command = new SqlCommand(cmdText, conn))
                    {
                        command.Transaction = tran;

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            Boolean isFirstReader = true;
                            do
                            {
                                Console.WriteLine("It's the result of {0} read:", isFirstReader ? "first" : "second");
                                TransactionIsolationLevels.DisplayData(reader);
                                isFirstReader = !isFirstReader;
                            } while (reader.NextResult() && !isFirstReader);
                        }
                    }

                    tran.Commit();
                }
            }

            Console.WriteLine("Exit from the NonrepeatableReadFirstThread.....");
        }
Пример #2
0
        static void Main(string[] args)
        {
            string connString = @"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;Asynchronous Processing=true;";

            OperateDatabase.CreateDatabase(connString);
            Console.WriteLine();

            Console.WriteLine("Demonstrate the ReadUncommitted transaction: ");
            TransactionIsolationLevels.DemonstrateIsolationLevel(connString,
                                                                 System.Data.IsolationLevel.ReadUncommitted);
            Console.WriteLine("-----------------------------------------------");

            Console.WriteLine("Demonstrate the ReadCommitted transaction: ");
            TransactionIsolationLevels.DemonstrateIsolationLevel(connString,
                                                                 System.Data.IsolationLevel.ReadCommitted);
            Console.WriteLine("-----------------------------------------------");

            Console.WriteLine("Demonstrate the RepeatableRead transaction: ");
            TransactionIsolationLevels.DemonstrateIsolationLevel(connString,
                                                                 System.Data.IsolationLevel.RepeatableRead);
            Console.WriteLine("-----------------------------------------------");

            Console.WriteLine("Demonstrate the Serializable transaction: ");
            TransactionIsolationLevels.DemonstrateIsolationLevel(connString,
                                                                 System.Data.IsolationLevel.Serializable);
            Console.WriteLine("-----------------------------------------------");

            Console.WriteLine("Demonstrate the Snapshot transaction: ");
            OperateDatabase.SetSnapshot(connString, true);
            TransactionIsolationLevels.DemonstrateIsolationLevel(connString,
                                                                 System.Data.IsolationLevel.Snapshot);
            Console.WriteLine("-----------------------------------------------");

            Console.WriteLine("Demonstrate the difference between the Snapshot and Serializable transactions:");
            TransactionIsolationLevels.DemonstrateBetweenSnapshotAndSerializable(connString);
            OperateDatabase.SetSnapshot(connString, false);
            Console.WriteLine();

            Console.WriteLine("Press ENTER to quit...");
            Console.ReadLine();
        }