Contoh membuat class pada C# adalah sebagai berikut
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
public class ApPembeli
{
private string _ckode;
private string _ckodefind;
private string _cnama;
private string _cusrupdate;
private string _dtgtlupdate;
public string Sort_On;
public ApPembeli() { }
public string ckode
{
get { return _ckode; }
set { _ckode = value; }
}
public string ckodefind
{
get { return _ckodefind; }
set { _ckodefind = value; }
}
public string cnama
{
get { return _cnama; }
set { _cnama = value; }
}
public string cusrupdate
{
get { return _cusrupdate; }
set { _cusrupdate = value; }
}
public string dtgtlupdate
{
get { return _dtgtlupdate; }
set { _dtgtlupdate = value; }
}
//mengambil data pada database dengan return dataset collection
public DataSet GetApPembeli()
{
string strSql = string.Empty;
strSql = "SELECT * FROM tblappembeli ";
strSql = strSql + " WHERE ckode like '%" + _ckode + "%'";
strSql = strSql + "and cnama like '%" + _cnama + "%'";
if (!string.IsNullOrEmpty(Sort_On))
{
strSql = strSql + " ORDER BY " + Sort_On;
}
else
{
strSql = strSql + " ORDER BY " + "ckode" + " ASC ";
}
DataSet dsTemp;
dsTemp = SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(), CommandType.Text, strSql);
return dsTemp;
}
//prosedur untuk menambah pembeli baru dengan perintah store procedure yang telah dibuat di sql server
public void AddApPembeli()
{
SqlParameter[] p = new SqlParameter[3];
p[0] = new SqlParameter("@ckode", SqlDbType.VarChar);
p[0].Value = _ckode;
p[1] = new SqlParameter("@cnama", SqlDbType.VarChar);
p[1].Value = _cnama;
p[2] = new SqlParameter("@cusrupdate", SqlDbType.VarChar);
p[2].Value = _cusrupdate;
SqlHelper.ExecuteNonQuery(ClsConnectionString.getConnectionString(), CommandType.StoredProcedure, "AP_SP_pembeli_avq", p);
}
//mengambil data dari dalam database dan memasukkannya kedalam object properties
public void GetApPembeliDetails()
{
SqlParameter[] p = new SqlParameter[1];
p[0] = new SqlParameter("@ckode", SqlDbType.VarChar);
p[0].Value = _ckode;
DataSet ds = SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(), CommandType.StoredProcedure, "AP_SP_pembeli_qf", p);
DataRowCollection drc = ds.Tables[0].Rows;
if (drc.Count > 0)
{
_ckode = drc[0]["ckode"].ToString();
_cnama = drc[0]["cnama"].ToString();
//_cusrupdate = drc[0]["cusrupdate"].ToString();
//_dtgtlupdate = drc[0]["dtgtlupdate"].ToString();
}
else
{
_ckode = "";
_cnama = "";
//_cusrupdate = "";
//_dtgtlupdate = "";
}
}
//fungsi untuk meng-update database
public void UpdateApPembeli()
{
SqlParameter[] p = new SqlParameter[4];
p[0] = new SqlParameter("@ckode", SqlDbType.VarChar);
p[0].Value = _ckode;
p[1] = new SqlParameter("@cnama", SqlDbType.VarChar);
p[1].Value = _cnama;
p[2] = new SqlParameter("@cusrupdate", SqlDbType.VarChar);
p[2].Value = _cusrupdate;
p[3] = new SqlParameter("@ckodefind", SqlDbType.VarChar);
p[3].Value = _ckodefind;
SqlHelper.ExecuteNonQuery(ClsConnectionString.getConnectionString(), CommandType.StoredProcedure, "AP_SP_pembeli_uq", p);
}
//fungsi untuk menghapus data pembeli
public void DeleteApPembeli()
{
SqlParameter[] p = new SqlParameter[2];
p[0] = new SqlParameter("@ckode", SqlDbType.VarChar);
p[0].Value = _ckode;
p[1] = new SqlParameter("@cusrupdate", SqlDbType.VarChar);
p[1].Value = _cusrupdate;
SqlHelper.ExecuteNonQuery(ClsConnectionString.getConnectionString(), CommandType.StoredProcedure, "AP_SP_pembeli_dq", p);
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
public class ApPembeli
{
private string _ckode;
private string _ckodefind;
private string _cnama;
private string _cusrupdate;
private string _dtgtlupdate;
public string Sort_On;
public ApPembeli() { }
public string ckode
{
get { return _ckode; }
set { _ckode = value; }
}
public string ckodefind
{
get { return _ckodefind; }
set { _ckodefind = value; }
}
public string cnama
{
get { return _cnama; }
set { _cnama = value; }
}
public string cusrupdate
{
get { return _cusrupdate; }
set { _cusrupdate = value; }
}
public string dtgtlupdate
{
get { return _dtgtlupdate; }
set { _dtgtlupdate = value; }
}
//mengambil data pada database dengan return dataset collection
public DataSet GetApPembeli()
{
string strSql = string.Empty;
strSql = "SELECT * FROM tblappembeli ";
strSql = strSql + " WHERE ckode like '%" + _ckode + "%'";
strSql = strSql + "and cnama like '%" + _cnama + "%'";
if (!string.IsNullOrEmpty(Sort_On))
{
strSql = strSql + " ORDER BY " + Sort_On;
}
else
{
strSql = strSql + " ORDER BY " + "ckode" + " ASC ";
}
DataSet dsTemp;
dsTemp = SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(), CommandType.Text, strSql);
return dsTemp;
}
//prosedur untuk menambah pembeli baru dengan perintah store procedure yang telah dibuat di sql server
public void AddApPembeli()
{
SqlParameter[] p = new SqlParameter[3];
p[0] = new SqlParameter("@ckode", SqlDbType.VarChar);
p[0].Value = _ckode;
p[1] = new SqlParameter("@cnama", SqlDbType.VarChar);
p[1].Value = _cnama;
p[2] = new SqlParameter("@cusrupdate", SqlDbType.VarChar);
p[2].Value = _cusrupdate;
SqlHelper.ExecuteNonQuery(ClsConnectionString.getConnectionString(), CommandType.StoredProcedure, "AP_SP_pembeli_avq", p);
}
//mengambil data dari dalam database dan memasukkannya kedalam object properties
public void GetApPembeliDetails()
{
SqlParameter[] p = new SqlParameter[1];
p[0] = new SqlParameter("@ckode", SqlDbType.VarChar);
p[0].Value = _ckode;
DataSet ds = SqlHelper.ExecuteDataset(ClsConnectionString.getConnectionString(), CommandType.StoredProcedure, "AP_SP_pembeli_qf", p);
DataRowCollection drc = ds.Tables[0].Rows;
if (drc.Count > 0)
{
_ckode = drc[0]["ckode"].ToString();
_cnama = drc[0]["cnama"].ToString();
//_cusrupdate = drc[0]["cusrupdate"].ToString();
//_dtgtlupdate = drc[0]["dtgtlupdate"].ToString();
}
else
{
_ckode = "";
_cnama = "";
//_cusrupdate = "";
//_dtgtlupdate = "";
}
}
//fungsi untuk meng-update database
public void UpdateApPembeli()
{
SqlParameter[] p = new SqlParameter[4];
p[0] = new SqlParameter("@ckode", SqlDbType.VarChar);
p[0].Value = _ckode;
p[1] = new SqlParameter("@cnama", SqlDbType.VarChar);
p[1].Value = _cnama;
p[2] = new SqlParameter("@cusrupdate", SqlDbType.VarChar);
p[2].Value = _cusrupdate;
p[3] = new SqlParameter("@ckodefind", SqlDbType.VarChar);
p[3].Value = _ckodefind;
SqlHelper.ExecuteNonQuery(ClsConnectionString.getConnectionString(), CommandType.StoredProcedure, "AP_SP_pembeli_uq", p);
}
//fungsi untuk menghapus data pembeli
public void DeleteApPembeli()
{
SqlParameter[] p = new SqlParameter[2];
p[0] = new SqlParameter("@ckode", SqlDbType.VarChar);
p[0].Value = _ckode;
p[1] = new SqlParameter("@cusrupdate", SqlDbType.VarChar);
p[1].Value = _cusrupdate;
SqlHelper.ExecuteNonQuery(ClsConnectionString.getConnectionString(), CommandType.StoredProcedure, "AP_SP_pembeli_dq", p);
}
}
Sekian terima kasih semoga bemanfaat
Post a Comment for "Membuat class pada C# dengan sql helper dan dataset "