Exemple #1
0
        public void TestMethod1()
        {
            //https://github.com/tmsmith/Dapper-Extensions
            //https://github.com/tmsmith/Dapper-Extensions/wiki/Predicates
            try
            {
                string _connectionString = "Data Source=.;Initial Catalog=SportsDB;Persist Security Info=True;User ID=sa;Password=11111111;Integrated Security=True";
                using (System.Data.IDbConnection cn = new System.Data.SqlClient.SqlConnection(_connectionString))
                {
                    cn.Open();

                    //添加
                    //var multiKey = cn.Insert<City>(new City() { CityName = "北京5" });
                    //Console.WriteLine(multiKey);

                    //分页

                    //分页查询
                    IList <ISort> sort = new List <ISort>();
                    sort.Add(new Sort {
                        PropertyName = "CityID", Ascending = false
                    });

                    long allRowsCount = 0;
                    //cn.GetPage<City>(1, 10, out allRowsCount, new { ID = 1 }, sort);
                    var predicate_page           = Predicates.Field <City>(f => f.CityName, Operator.Like, "%北京%");
                    IEnumerable <City> list_page = cn.GetList <City>(predicate_page);

                    var result = cn.GetPage <City>(predicate_page, sort, 1, 1);
                    allRowsCount = cn.Count <City>(predicate_page);
                    Console.WriteLine(result.AsList().Count);

                    //查询
                    var predicate           = Predicates.Field <City>(f => f.CityName, Operator.Eq, "北京2");
                    IEnumerable <City> list = cn.GetList <City>(predicate);
                    Console.WriteLine(list.AsList()[0].CityName);

                    //查询
                    int  cityId = 1;
                    City person = cn.Get <City>(cityId);

                    person.CityName = "上海";
                    cn.Update(person);

                    cn.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }