C#工具类----ormlite处理插入数据

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43906877/article/details/100631799

利用servicestack的ormlite,进行插入model对象

利用反射,对将要插入的对象的某些属性的value进行处理

public void opencon<T>(List<T> list)
        {
            //通过反射,获得t的属性,判断是否存在coursetime和courseplace,如果存在则将其属性值拿出来,然后使用字符串处理工具,将其处理后在放到数据库
            var dbfactory=new OrmLiteConnectionFactory(connstring,SqlServer2008OrmLiteDialectProvider.Instance);
            using (var db = dbfactory.Open())
            {
                //db.Insert(student);
                //利用反射,观察该t型对象是不是course
                var pros = typeof(T).GetProperties();
                //用于计数,计foerach的次数
                int count = 0;
                foreach (var VARIABLE in pros)
                {
                    PropertyInfo p = typeof(Course).GetProperty("CoursePlace");
                    if (VARIABLE==p)
                    {
                        //object obj = p.GetValue(list, null);
                        //将list中的值读取出来
                        object value = p.GetValue(list[count]);
                        //字符串的处理
                        //StringDeal sc=new StringDeal(value.ToString());
                        //value=sc.Adress();
                        //处理后将值赋给list
                        p.SetValue(list[count], value);
                    }
                }
                foreach (var VARIABLE in pros)
                {
                    PropertyInfo p = typeof(Course).GetProperty("CourseTime");
                    //插入到数据库之前,对字符串进行处理
                    if (VARIABLE == p)
                    {
                        //object obj = p.GetValue(list, null);
                        //将list中的值读取出来
                        object value = p.GetValue(list[count]);
                        //字符串的处理
                        //StringDeal sc = new StringDeal(value.ToString());
                        //value = sc.Adress();
                        //处理后将值赋给list
                        p.SetValue(list[count], value);
                    }
                }
                for (int i = 0; i < list.Count; i++)
                {
                    db.Insert(list[i]);
                }
            }
        }

猜你喜欢

转载自blog.csdn.net/weixin_43906877/article/details/100631799