LinQ insert delete update

Standar

14. Tutor ADO.NET Entity Framework
Tutorial ADO.NET Entity Framework untuk proses transaksi ORM (Object Relational Mapping) yang
berguna untuk menyimpan data dari class diagram (object) ke dalam sebuah database egine server.
Dibawah ini adalah contoh proses Insert, Edit, Delete object EntityFramework :
///

InsertUser

/// Collections of MstUser table
/// Success if its true
public static bool InsertUser(MstUser user)
{
bool result = false;
if (user != null)
{
try
{
using (NYKEntities ledger = new NYKEntities())
{
var getUser = GetUserByUserLogin(user.UserLogin);
if (getUser != null)
{
throw new ArgumentException(“User” + ” ” + “‘” +
user.UserName + “sudah terdaftar”);
}
else
{
ledger.MstUsers.AddObject(user);
int succes = ledger.SaveChanges();
result = succes > 0;
}
}
}
catch (Exception)
{
throw;
}
}
return result;
}
///

UpdateUser

/// Colllections of MstUser table
/// Success if its true

bool result = false;
if (user != null)
{
try
{
using (NYKEntities ledger = new NYKEntities())
{
IQueryable Query = from usr in ledger.MstUsers
where usr.UserPk == user.UserPk
select usr;
if (Query.Count() > 0)
{
object temp = Query.First();
ledger.MstUsers.ApplyCurrentValues(user);
ledger.DetectChanges();
int succces = ledger.SaveChanges();
result = succces > 0;
}
}
}
catch (Exception)
{
throw;
}
}
return result;
}
///

DeleteExchangeRate

/// Collections of TrnExchangeRate
/// Success if its true

public static bool DeleteExchangeRate(TrnExchangeRate rate)
{
bool result = false;
if (rate != null)
{
try
{
using (NYKEntities ledger = new NYKEntities())
{
var delObj = ledger.TrnExchangeRates.Where(s => s.ExchangeRatePk
== rate.ExchangeRatePk).First();
ledger.DeleteObject(delObj);
int changes = ledger.SaveChanges();
result = changes
public static bool UpdateUser(MstUser user)
{
throw;
}
}
return result;
}

public static List GetUserNameForAdmin(String _userName)
{
List lstUser = new List();

if (!String.IsNullOrEmpty(_userName))
{
using (NYKEntities ledger = new NYKEntities())
{
IQueryable Query = from usr in ledger.MstUsers
where usr.UserRole > 1 && usr.UserName.Contains(_userName)
&& usr.CanAccesBillingPlus == true
orderby usr.UserRole ascending
select new MasterUser
{
userLogin = usr.UserLogin,
userName = usr.UserName,
userJabatan = usr.UserJabatan,
userRole = usr.UserRole,
userOfficeDepartment = usr.UserOfficeDepartment,
userIsActive = usr.UserIsActive,

};

List lookUpUser = new List(Query);

if (lookUpUser.Count() > 0)
{
foreach (var item in lookUpUser)
{
if (item.userIsActive)
{
item.Status = “available.png”;
}
else
{
item.Status = “no_entry.png”;
}
item.roleName = Enum.GetName(typeof(EnumarationRole), item.userRole);
item.departementName = Enum.GetName(typeof(EnumarationDepartement), item.userOfficeDepartment);
}
}

lstUser = new List(lookUpUser);

}
}

return lstUser;

}

public static MstUser GetUserByUserPk(Int64 _userPk)
{
MstUser result = null;

if (_userPk > 0)
{
using (NYKEntities ledger = new NYKEntities())
{
IQueryable Query = from usr in ledger.MstUsers
where usr.UserPk == _userPk
select usr;
List lstUser = new List(Query);

if (lstUser.Count() > 0)
{
result = lstUser.First();
}
}
}

return result;
}
Cara pemanggilan fungsi – fungsi di atas :
///

BindBackFromControl

/// Collections of MstUser table
private MstUser BindBackFromControl()
{
String _pass = MasterUser.GetHash(this.txtPassword.Text);
Int32 _role = Convert.ToInt32(this.ddlRole.SelectedValue);
Int32 _depart = Convert.ToInt32(this.ddlDepartment.SelectedValue);
String _login = this.txtUserLogin.Text.Trim();
String _name = this.txtNama.Text.Trim();
String _jabatan = this.txtJabatan.Text.Trim();
Boolean _isActive = Convert.ToBoolean(this.rblStatus.SelectedValue);
Boolean _isLedger = Convert.ToBoolean(this.rblLedgerPlus.SelectedValue);
Boolean _isBilling = Convert.ToBoolean(this.rblBilling.SelectedValue);
MstUser user = new MstUser();
user.UserPk = -1;
user.UserPassword = _pass;
user.UserName = _name;
user.UserLogin = _login;
user.UserJabatan = _jabatan;
user.UserOfficeDepartment = _depart;
user.UserRole = _role;
user.UserIsActive = _isActive;
user.CanAccesLedgerPlus = _isLedger;
user.CanAccesBillingPlus = _isBilling;
return user;
}

///

SaveProcess

private void SaveProcess()
{
MstUser user = this.BindBackFromControl();
bool result = MasterUser.InsertUser(user);
if (result)
{
this.lblPesan.Text = ErrorConstantas.ERR_SAVE_SUCCESS;
this.lblPesan.Visible = true;
this.lblPesan.ForeColor = Color.Red;
this.lblPesan.Font.Size = 10;
this.lblPesan.Font.Italic = true;
}
else
{
this.lblPesan.Text = ErrorConstantas.ERR_SAVE_FAILED;
this.lblPesan.Visible = true;
this.lblPesan.ForeColor = Color.Red;
this.lblPesan.Font.Size = 10;
this.lblPesan.Font.Italic = true;
}
}
private void BindToControl()
{
int _role = 0;

if (this._userPk > 0)
{
var user = MasterUser.GetUserByUserPk(this._userPk);

if (user != null)
{
this.txtUserLogin.Text = user.UserLogin;
this.txtNama.Text = user.UserName;
_role = this._userRole;

this.ddlRole.SelectedIndex = _role;
this.IsiRole((EnumarationRole)_role);

this.ddlRole.SelectedValue = user.UserRole.ToString();

if (this._userRole == (short)EnumarationRole.Administrator
|| this._userRole == (short)EnumarationRole.Supervisor_HRD)
{

this.ddlRole.Enabled = true;
}
else
{
this.ddlRole.Enabled = false;
}

this.IsiDepartement((EnumarationDepartement)user.UserOfficeDepartment);

this.ddlDepartement.SelectedValue = user.UserOfficeDepartment.ToString();
this.ddlDepartement.Enabled = false;

this.rblStatus.SelectedValue = user.UserIsActive.ToString();
this.txtJabatan.Text = user.UserJabatan;

}
}
}

private void BindToGridUser()
{

#region Obout Grid

String search = this.txtName.Text;

if (!string.IsNullOrEmpty(search))
{
this.lookUser = MasterUser.GetUserNameForAdmin(search);

}
else
{
this.lookUser = MasterUser.GetAllUserForAdmin();
}

this.grdUser.PageSizeOptions = “10,15”;
this.grdUser.AllowAddingRecords = false;
this.grdUser.DataSource = null;
this.grdUser.DataSource = this.lookUser;
this.grdUser.DataBind();

#endregion
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s