码迷,mamicode.com
首页 > 移动开发 > 详细

csharp:Dapper Sample

时间:2015-07-26 06:05:54      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

You can find Dapper on Google Code here: http://code.google.com/p/dapper-dot-net/ and the GitHub distro here: https://github.com/SamSaffron/dapper-dot-net.

        ///<summary>
        /// 追加记录
        ///</summary>
        ///<param name="BookPlaceListInfo"></param>
        ///<returns></returns>
        public int InsertBookPlaceList(BookPlaceListInfo bookPlaceList)
        {
            int ret = 0;
            try
            {
                List<BookPlaceListInfo> list=new List<BookPlaceListInfo>();
                list.Add(bookPlaceList);
                StringBuilder str = new StringBuilder();
                str.Append("INSERT INTO BookPlaceList ");
                str.Append("([BookPlaceName] ,[BookPlaceCode] ,[BookPlaceParent]) VALUES ");
                str.Append("(@BookPlaceName ,@BookPlaceCode,@BookPlaceParent)");
                ret=SqlMapperUtil.InsertMultiple<BookPlaceListInfo>(str.ToString(),list, SqlHelper.ConnectionString);

            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return ret;
        }
        /// <summary>
        /// 追加多条记录
        /// </summary>
        /// <param name="bookPlaceList"></param>
        /// <returns></returns>
        public int InsertBookPlaceListMore(List<BookPlaceListInfo> bookPlaceList)
        {
            int ret = 0;
            try
            {
   
                StringBuilder str = new StringBuilder();
                str.Append("INSERT INTO BookPlaceList ");
                str.Append("([BookPlaceName] ,[BookPlaceCode] ,BookPlaceParent]) VALUES ");
                str.Append("(@BookPlaceName ,@BookPlaceCode,@BookPlaceParent)");
                ret = SqlMapperUtil.InsertMultiple<BookPlaceListInfo>(str.ToString(), bookPlaceList, SqlHelper.ConnectionString);

            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return ret;
        }

        ///<summary>
        ///修改记录
        ///</summary>
        ///<param name="BookPlaceListInfo"></param>
        ///<returns></returns>
        public int UpdateBookPlaceList(BookPlaceListInfo bookPlaceList)
        {
            int ret = 0;
            try
            {
                List<BookPlaceListInfo> list = new List<BookPlaceListInfo>();
                list.Add(bookPlaceList);
                StringBuilder str = new StringBuilder();
                str.Append("UPDATE BookPlaceList SET ");
                str.Append("[BookPlaceName]=@BookPlaceName ,");
                str.Append("[BookPlaceCode]=@BookPlaceCode,");
                str.Append("[BookPlaceParent]=@BookPlaceParent");
                str.Append(" where ");
                str.Append("[BookPlaceID]=@BookPlaceID");
                ret = SqlMapperUtil.InsertMultiple<BookPlaceListInfo>(str.ToString(), list, SqlHelper.ConnectionString);

            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return ret;
        }
        ///<summary>
        /// 删除记录
        ///</summary>
        ///<param name="bookPlaceIDInfo"></param>
        ///<returns></returns>
        public bool DeleteBookPlaceList(int bookPlaceID)
        {
            bool ret = false;
            try
            {
                int temp = 0;
                StringBuilder str = new StringBuilder();
                str.Append("DELETE	BookPlaceList WHERE BookPlaceID = @BookPlaceID");
                temp = SqlMapperUtil.InsertUpdateOrDeleteSql(str.ToString(), new { BookPlaceID = bookPlaceID }, SqlHelper.ConnectionString);
                
                if (temp != 0)
                {
                    ret = true;
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return ret;
        }
        ///<summary>
        /// 查询记录
        ///</summary>
        ///<param name="bookPlaceIDInfo"></param>
        ///<returns></returns>
        public BookPlaceListInfo SelectBookPlaceList(int bookPlaceID)
        {
            BookPlaceListInfo bookPlaceList = null;
            try
            {
                StringBuilder str = new StringBuilder();
                str.Append("SELECT * FROM BookPlaceList WHERE BookPlaceID = @BookPlaceID");
                bookPlaceList = SqlMapperUtil.SqlWithParamsSingle<BookPlaceListInfo>(str.ToString(), new { BookPlaceID = bookPlaceID }, SqlHelper.ConnectionString);

            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return bookPlaceList;
        }

        ///<summary>
        /// 查询所有记录
        ///</summary>
        ///<returns></returns>
        public List<BookPlaceListInfo> SelectBookPlaceListAll()
        {
            List<BookPlaceListInfo> list = new List<BookPlaceListInfo>();          
            try
            {
                string str = "SELECT * FROM BookPlaceList";
                list = SqlMapperUtil.SqlWithParams<BookPlaceListInfo>(str, null, SqlHelper.ConnectionString);
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            return list;
        }

  与SqlHelper比批量数据插入快近一半

csharp:Dapper Sample

标签:

原文地址:http://www.cnblogs.com/geovindu/p/4676972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!