/// <summary> /// 批量添加实体 /// </summary> /// <param name="entityList">要创建的实体</param> /// <param name="connectionString">链接字符串</param> /// <returns></returns> public bool CreateEntityList(IEnumerable <User> entityList, string connectionString = null) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection(connectionString)) { string insertSql = @"INSERT INTO [dbo].[User] ([UserName] ,[Password] ,[Gender] ,[Birthday] ,[CreateUserId] ,[CreateDate] ,[UpdateUserId] ,[UpdateDate] ,[IsDeleted]) VALUES (@UserName ,@Password ,@Gender ,@Birthday ,@CreateUserId ,@CreateDate ,@UpdateUserId ,@UpdateDate ,@IsDeleted)"; return(conn.Execute(insertSql, entityList) > 0); } }
/// <summary> /// 创建一个产品 /// </summary> /// <param name="entity">产品</param> /// <param name="connectionString">链接字符串</param> /// <returns></returns> public bool CreateEntityList(IEnumerable <Product> entityList, string connectionString = null) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection(connectionString)) { string insertSql = @"INSERT INTO [dbo].[Product] ([ProductName] ,[Brand] ,[ProductPrice] ,[CreateUserId] ,[CreateDate] ,[UpdateUserId] ,[UpdateDate] ,[IsDeleted]) VALUES (@ProductName ,@Brand ,@ProductPrice ,@CreateUserId ,@CreateDate ,@UpdateUserId ,@UpdateDate ,@IsDeleted)"; return(conn.Execute(insertSql, entityList) > 0); } }
/// <summary> /// 根据主键Id删除一个产品 /// </summary> /// <param name="id">主键Id</param> /// <param name="connectionString">链接字符串</param> /// <returns></returns> public bool DeleteEntityById(int id, string connectionString = null) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection(connectionString)) { string deleteSql = @"DELETE FROM [dbo].[Product] WHERE [Id] = @Id"; return(conn.Execute(deleteSql, new { Id = id }) > 0); } }
/// <summary> /// 修改一个产品 /// </summary> /// <param name="entity">要修改的产品</param> /// <param name="connectionString">链接字符串</param> /// <returns></returns> public bool UpdateEntity(Product entity, string connectionString = null) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection(connectionString)) { string updateSql = @"UPDATE [dbo].[Product] SET [ProductName]=@ProductName ,[Brand]=@Brand ,[CreateUserId]=@CreateUserId ,[UpdateDate]=@UpdateDate ,[UpdateUserId]=@UpdateUserId ,[CreateDate]=@CreateDate ,[IsDeleted]=@IsDeleted WHERE Id = @Id"; return(conn.Execute(updateSql, entity) > 0); } }
/// <summary> /// 根据主键Id获取一个产品 /// </summary> /// <param name="id">主键Id</param> /// <param name="connectionString">链接字符串</param> /// <returns></returns> public Product RetriveOneEntityById(int id, string connectionString = null) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection(connectionString)) { string querySql = @"SELECT([ProductName] ,[Brand] ,[ProductPrice] ,[CreateUserId] ,[CreateDate] ,[UpdateUserId] ,[UpdateDate] ,[IsDeleted]) FROM [dbo].[Product] WHERE Id = @Id"; return(conn.QueryFirstOrDefault <Product>(querySql, new { Id = id })); } }
/// <summary> /// 修改一个用户 /// </summary> /// <param name="entity">要修改的用户</param> /// <param name="connectionString">链接字符串</param> /// <returns></returns> public bool UpdateEntity(User entity, string connectionString = null) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection(connectionString)) { string updateSql = @"UPDATE [dbo].[User] SET [UserName] = @UserName ,[Password] = @Password ,[Gender] = @Gender ,[Birthday] = @Birthday ,[UpdateUserId] = @UpdateUserId ,[UpdateDate] = @UpdateDate ,[IsDeleted] = @IsDeleted WHERE Id = @Id"; return(conn.Execute(updateSql, entity) > 0); } }
/// <summary> /// 获取所有产品 /// </summary> /// <param name="connectionString">链接字符串</param> /// <returns></returns> public IEnumerable <Product> RetriveAllEntity(string connectionString = null) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection(connectionString)) { string querySql = @"SELECT [Id] ,[ProductName] ,[Brand] ,[ProductPrice] ,[CreateUserId] ,[CreateDate] ,[UpdateUserId] ,[UpdateDate] ,[IsDeleted] FROM [dbo].[Product]"; return(conn.Query <Product>(querySql)); } }
/// <summary> /// 获取所有用户 /// </summary> /// <param name="connectionString">链接字符串</param> /// <returns></returns> public IEnumerable <User> RetriveAllEntity(string connectionString = null) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection(connectionString)) { string querySql = @"SELECT [Id] ,[UserName] ,[Password] ,[Gender] ,[Birthday] ,[CreateUserId] ,[CreateDate] ,[UpdateUserId] ,[UpdateDate] ,[IsDeleted] FROM [dbo].[User]"; return(conn.Query <User>(querySql)); } }
/// <summary> /// 根据主键Id获取一个用户 /// </summary> /// <param name="id">主键Id</param> /// <param name="connectionString">链接字符串</param> /// <returns></returns> public User RetriveOneEntityById(int id, string connectionString = null) { using (IDbConnection conn = DataBaseConfig.GetSqlConnection(connectionString)) { string querySql = @"SELECT [Id] ,[UserName] ,[Password] ,[Gender] ,[Birthday] ,[CreateUserId] ,[CreateDate] ,[UpdateUserId] ,[UpdateDate] ,[IsDeleted] FROM [dbo].[User] WHERE Id = @Id"; return(conn.QueryFirstOrDefault <User>(querySql, new { Id = id })); } }