コード例 #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.....");
        }