你的位置:首页 > ASP.net教程

[ASP.net教程]csharp:Dapper Sample


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比批量数据插入快近一半