TransactionScope小例

Posted zslm___

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TransactionScope小例相关的知识,希望对你有一定的参考价值。

1

public static class DataTableHelper
    {
        public static List<T> ToModel<T>(this DataTable dt) where T : class ,new()
        {
            if (dt == null || dt.Rows.Count == 0)
                return null;
            Type type = typeof(T);
            var list = new List<T>();
            for (int row = 0; row < dt.Rows.Count; row++)
            {
                Object obj = type.Assembly.CreateInstance(type.FullName);

                System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();

                for (int i = 0; i < Props.Length; i++)
                {
                    if (Props[i].CanWrite && dt.Columns.IndexOf(Props[i].Name) > -1)
                    {
                        Props[i].SetValue(obj, dt.Rows[row][Props[i].Name], null);
                    }
                }

                list.Add(obj as T);
            }

            return list;
        }
    }
    public class StudentModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime CreateTime { get; set; }
    }
    //数据库访问对象
    public class Studuent
    {
        public static IList<StudentModel> GetList()
        {
            //不允许脏读
            string sql = "SELECT Id,Name,CreateTime FROM Student ORDER BY Id DESC;";
            DataTable dt = SqlHelper.Instance.ExecuteDataTable(CommandType.Text, sql);
            return dt.ToModel<StudentModel>();
        }

        public static bool Add(string name)
        {
            SqlParameter[] parms = { 
                                       new SqlParameter("@Name", SqlDbType.NVarChar, 32) { Value = name }, 
                                       new SqlParameter("@CreateTime", SqlDbType.DateTime) { Value = DateTime.Now }
                                   };
            string sql = "INSERT INTO Student (Name,CreateTime) VALUES (@Name,@CreateTime)";
            return SqlHelper.Instance.ExecuteNonQuery(CommandType.Text, sql, parms) > 0;
        }

        public static void Clear()
        {
            SqlHelper.Instance.ExecuteNonQuery("DELETE From Student");
        }

        //公共方法,输出学生列表

    }
    public class TransactionScopeTest
    {
        static void PrintStudent()
        {
            IList<StudentModel> list = Studuent.GetList();
            if (list == null)
                return;
            foreach (var item in list)
            {
                Console.WriteLine("{0}\t{1}\t{2}", Thread.CurrentThread.ManagedThreadId, item.Name, item.CreateTime);
            }
            Console.WriteLine();
        }

        public static void Excute()
        {
            Studuent.Clear();
            Task.Run(() =>
            {
                Console.WriteLine("开始添加用户");
                using (TransactionScope scope = new TransactionScope())
                {
                    Studuent.Add("Grace");
                    Studuent.Add("Aven");
                    scope.Complete();
                    //在Compltete已提交数据库
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "Scope已提交");
                    Thread.Sleep(1000 * 10);
                }

                //在作用范围外解除锁表
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "scope已释放");
            });
            //PrintStudent();
        }
    }

 

以上是关于TransactionScope小例的主要内容,如果未能解决你的问题,请参考以下文章

代码生成器小例

为啥 TransactionScope 不能与 Sqlite 一起使用?

[aspx]控件及代码小例

忽略特定查询的 TransactionScope

Transactionscope .net 核心 2

嵌套 TransactionScope 在测试中失败