Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
using ComManager.Models;
using Dapper;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace ComManager.Repository
{
public class CompanyRepository
: IRepository<Company>
{
private IDbConnection _db = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
public int Create(Company t)
{
var sqlQuery = "INSERT INTO COR.COMPANY (NAME, DESCRIPTION,IS_ACTIVE) VALUES(@Name, @Description, @IsActive); " +
"SELECT CAST(SCOPE_IDENTITY() as int)";
var id = this._db.Query<int>(sqlQuery, t).Single();
t.Id = id;
return t.Id;
}
public int Delete()
{
var sqlQuery = ("DELETE FROM COR.COMPANY");
return _db.Execute(sqlQuery);
}
public int Delete(int id)
{
var sqlQuery = ("DELETE FROM COR.COMPANY WHERE ID=" + id + "");
return _db.Execute(sqlQuery);
}
public Company Find(int id)
{
string query = "SELECT * FROM COR.COMPANY(NOLOCK) WHERE ID = " + id + "";
return _db.Query<Company>(query).SingleOrDefault();
}
public List<Company> List()
{
List<Company> list = this._db.Query<Company>("SELECT * FROM COR.COMPANY(NOLOCK)").ToList();
return list;
}
public bool Update(Company t)
{
var sqlQuery =
"UPDATE COR.COMPANY " +
"SET NAME = @Name, " +
" DESCRIPTION = @Description, " +
" IS_ACTIVE = @IsActive " +
"WHERE ID = @Id";
_db.Execute(sqlQuery, t);
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment