Berikut ini adalah cara membuat class dan menampung class tersebut ke dalam sebuah collection
menggunakan database mysql dan mysqlclient sebagai koneksinya..
di bawah ini misalnya adalah class product
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using MySql.Data.MySqlClient;
namespace POS_DLL
{
public class Product
{
private string _kodeProduk;
private string _namaProduk;
private string _kategori;
private double _hargaBeli;
private double _harga;
private string _remark;
private double _terjual;
private bool _isNew=true;
private static string SQL_GET = "SELECT * FROM Products WHERE kodeproduk=@kodeproduk ";
private static string SQL_ADD = "INSERT INTO Products(kodeProduk, namaProduk, kategori, harga, remark, terjual,hargabeli)VALUES (@kodeProduk, @namaProduk, @kategori, @harga, @remark, @terjual,@hargabeli)";
private static string SQL_UPDATE = " UPDATE Products SET namaProduk =@namaProduk, kategori =@kategori, harga =@harga, remark =@remark, terjual =@terjual,hargabeli=@hargabeli WHERE kodeProduk =@kodeProduk ";
private static string SQL_DELETE = "DELETE FROM Products WHERE kodeProduk=@kodeProduk ";
public string Sort_On;
public Product() { }
public string KodeProduk
{
get { return _kodeProduk; }
set { _kodeProduk = value; }
}
public string namaProduk
{
get { return _namaProduk; }
set { _namaProduk = value; }
}
public string Kategori
{
get { return _kategori; }
set { _kategori = value; }
}
public double HargaBeli
{
get { return _hargaBeli; }
set { _hargaBeli = value; }
}
public double Harga
{
get { return _harga; }
set { _harga = value; }
}
public string Remark
{
get { return _remark; }
set { _remark = value; }
}
public double Terjual
{
get { return _terjual; }
set { _terjual = value; }
}
public bool IsNew
{
get { return _isNew; }
set { _isNew= value; }
}
public void GetProductDetails()
{
DataSet dsTemp = new DataSet();
MySqlConnection Conn = new MySqlConnection(ConnectionString.getConnectionString());
using (MySqlCommand cmd = new MySqlCommand(SQL_GET, Conn))
{
cmd.Parameters.AddWithValue("@kodeProduk", _kodeProduk);
using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
{
da.Fill(dsTemp);
}
}
DataRowCollection drc = dsTemp.Tables[0].Rows;
if (drc.Count > 0)
{
_kodeProduk = drc[0]["kodeProduk"].ToString();
_namaProduk = drc[0]["namaProduk"].ToString();
_kategori = drc[0]["kategori"].ToString();
_hargaBeli = Convert.ToDouble(drc[0]["hargabeli"].ToString());
_harga = Convert.ToDouble(drc[0]["harga"].ToString());
_remark = drc[0]["remark"].ToString();
_terjual = Convert.ToDouble(drc[0]["terjual"].ToString());
_isNew = false;
}
else
{
_kodeProduk = "";
_namaProduk = "";
_kategori = "";
_harga = 0;
_hargaBeli = 0;
_remark = "";
_terjual = 0;
_isNew = true;
}
}
public void AddProduct()
{
using (MySqlConnection con = new MySqlConnection(ConnectionString.getConnectionString()))
{
con.Open();
using (MySqlCommand command = new MySqlCommand(SQL_ADD, con))
{
command.Parameters.Add(new MySqlParameter("kodeProduk", _kodeProduk));
command.Parameters.Add(new MySqlParameter("namaProduk", _namaProduk));
command.Parameters.Add(new MySqlParameter("kategori", _kategori));
command.Parameters.Add(new MySqlParameter("harga", _harga));
command.Parameters.Add(new MySqlParameter("remark", _remark));
command.Parameters.Add(new MySqlParameter("terjual", _terjual));
command.Parameters.Add(new MySqlParameter("hargabeli", _hargaBeli));
command.ExecuteNonQuery();
}
}
}
public void UpdateProduct()
{
using (MySqlConnection con = new MySqlConnection(ConnectionString.getConnectionString()))
{
con.Open();
using (MySqlCommand command = new MySqlCommand(SQL_UPDATE, con))
{
command.Parameters.Add(new MySqlParameter("kodeProduk", _kodeProduk));
command.Parameters.Add(new MySqlParameter("namaProduk", _namaProduk));
command.Parameters.Add(new MySqlParameter("kategori", _kategori));
command.Parameters.Add(new MySqlParameter("hargabeli", _hargaBeli));
command.Parameters.Add(new MySqlParameter("harga", _harga));
command.Parameters.Add(new MySqlParameter("remark", _remark));
command.Parameters.Add(new MySqlParameter("terjual", _terjual));
command.ExecuteNonQuery();
}
}
}
public void DeleteProduct()
{
using (MySqlConnection con = new MySqlConnection(ConnectionString.getConnectionString()))
{
con.Open();
using (MySqlCommand command = new MySqlCommand(SQL_DELETE, con))
{
command.Parameters.Add(new MySqlParameter("@kodeProduk", _kodeProduk));
command.ExecuteNonQuery();
}
}
}
}
}
untuk menampung class di atas kita menggunakan
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using MySql.Data.MySqlClient;
namespace POS_DLL
{
public class ProductList
{
private List<Product> _products = new List<Product>();
private static string SQL_GET = "SELECT * FROM products order by namaProduk ASC";
public List<Product> Products
{
get { return _products; }
set { _products = value; }
}
public void getAllProduct()
{
MySqlConnection Conn = new MySqlConnection(ConnectionString.getConnectionString());
using (MySqlCommand cmd = new MySqlCommand(SQL_GET, Conn))
{
Conn.Open();
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Product obj = new Product();
obj.KodeProduk = reader["kodeProduk"].ToString();
obj.namaProduk = reader["namaProduk"].ToString();
obj.Kategori = reader["kategori"].ToString();
obj.Harga = Convert.ToDouble(reader["harga"].ToString());
obj.Remark = reader["remark"].ToString();
obj.Terjual = Convert.ToDouble(reader["terjual"].ToString());
obj.IsNew = false;
_products.Add(obj);
}
}
}
}
}
//untuk memanggil di form menggunakan
ProductList _objProduct = new ProductList();
_objProduct.getAllProduct();
//looping object
foreach (Product p in _objProduct.Products) {
}
sekian terima kasih. semoga bermanfaat.
//
menggunakan database mysql dan mysqlclient sebagai koneksinya..
di bawah ini misalnya adalah class product
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using MySql.Data.MySqlClient;
namespace POS_DLL
{
public class Product
{
private string _kodeProduk;
private string _namaProduk;
private string _kategori;
private double _hargaBeli;
private double _harga;
private string _remark;
private double _terjual;
private bool _isNew=true;
private static string SQL_GET = "SELECT * FROM Products WHERE kodeproduk=@kodeproduk ";
private static string SQL_ADD = "INSERT INTO Products(kodeProduk, namaProduk, kategori, harga, remark, terjual,hargabeli)VALUES (@kodeProduk, @namaProduk, @kategori, @harga, @remark, @terjual,@hargabeli)";
private static string SQL_UPDATE = " UPDATE Products SET namaProduk =@namaProduk, kategori =@kategori, harga =@harga, remark =@remark, terjual =@terjual,hargabeli=@hargabeli WHERE kodeProduk =@kodeProduk ";
private static string SQL_DELETE = "DELETE FROM Products WHERE kodeProduk=@kodeProduk ";
public string Sort_On;
public Product() { }
public string KodeProduk
{
get { return _kodeProduk; }
set { _kodeProduk = value; }
}
public string namaProduk
{
get { return _namaProduk; }
set { _namaProduk = value; }
}
public string Kategori
{
get { return _kategori; }
set { _kategori = value; }
}
public double HargaBeli
{
get { return _hargaBeli; }
set { _hargaBeli = value; }
}
public double Harga
{
get { return _harga; }
set { _harga = value; }
}
public string Remark
{
get { return _remark; }
set { _remark = value; }
}
public double Terjual
{
get { return _terjual; }
set { _terjual = value; }
}
public bool IsNew
{
get { return _isNew; }
set { _isNew= value; }
}
public void GetProductDetails()
{
DataSet dsTemp = new DataSet();
MySqlConnection Conn = new MySqlConnection(ConnectionString.getConnectionString());
using (MySqlCommand cmd = new MySqlCommand(SQL_GET, Conn))
{
cmd.Parameters.AddWithValue("@kodeProduk", _kodeProduk);
using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
{
da.Fill(dsTemp);
}
}
DataRowCollection drc = dsTemp.Tables[0].Rows;
if (drc.Count > 0)
{
_kodeProduk = drc[0]["kodeProduk"].ToString();
_namaProduk = drc[0]["namaProduk"].ToString();
_kategori = drc[0]["kategori"].ToString();
_hargaBeli = Convert.ToDouble(drc[0]["hargabeli"].ToString());
_harga = Convert.ToDouble(drc[0]["harga"].ToString());
_remark = drc[0]["remark"].ToString();
_terjual = Convert.ToDouble(drc[0]["terjual"].ToString());
_isNew = false;
}
else
{
_kodeProduk = "";
_namaProduk = "";
_kategori = "";
_harga = 0;
_hargaBeli = 0;
_remark = "";
_terjual = 0;
_isNew = true;
}
}
public void AddProduct()
{
using (MySqlConnection con = new MySqlConnection(ConnectionString.getConnectionString()))
{
con.Open();
using (MySqlCommand command = new MySqlCommand(SQL_ADD, con))
{
command.Parameters.Add(new MySqlParameter("kodeProduk", _kodeProduk));
command.Parameters.Add(new MySqlParameter("namaProduk", _namaProduk));
command.Parameters.Add(new MySqlParameter("kategori", _kategori));
command.Parameters.Add(new MySqlParameter("harga", _harga));
command.Parameters.Add(new MySqlParameter("remark", _remark));
command.Parameters.Add(new MySqlParameter("terjual", _terjual));
command.Parameters.Add(new MySqlParameter("hargabeli", _hargaBeli));
command.ExecuteNonQuery();
}
}
}
public void UpdateProduct()
{
using (MySqlConnection con = new MySqlConnection(ConnectionString.getConnectionString()))
{
con.Open();
using (MySqlCommand command = new MySqlCommand(SQL_UPDATE, con))
{
command.Parameters.Add(new MySqlParameter("kodeProduk", _kodeProduk));
command.Parameters.Add(new MySqlParameter("namaProduk", _namaProduk));
command.Parameters.Add(new MySqlParameter("kategori", _kategori));
command.Parameters.Add(new MySqlParameter("hargabeli", _hargaBeli));
command.Parameters.Add(new MySqlParameter("harga", _harga));
command.Parameters.Add(new MySqlParameter("remark", _remark));
command.Parameters.Add(new MySqlParameter("terjual", _terjual));
command.ExecuteNonQuery();
}
}
}
public void DeleteProduct()
{
using (MySqlConnection con = new MySqlConnection(ConnectionString.getConnectionString()))
{
con.Open();
using (MySqlCommand command = new MySqlCommand(SQL_DELETE, con))
{
command.Parameters.Add(new MySqlParameter("@kodeProduk", _kodeProduk));
command.ExecuteNonQuery();
}
}
}
}
}
untuk menampung class di atas kita menggunakan
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using MySql.Data.MySqlClient;
namespace POS_DLL
{
public class ProductList
{
private List<Product> _products = new List<Product>();
private static string SQL_GET = "SELECT * FROM products order by namaProduk ASC";
public List<Product> Products
{
get { return _products; }
set { _products = value; }
}
public void getAllProduct()
{
MySqlConnection Conn = new MySqlConnection(ConnectionString.getConnectionString());
using (MySqlCommand cmd = new MySqlCommand(SQL_GET, Conn))
{
Conn.Open();
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Product obj = new Product();
obj.KodeProduk = reader["kodeProduk"].ToString();
obj.namaProduk = reader["namaProduk"].ToString();
obj.Kategori = reader["kategori"].ToString();
obj.Harga = Convert.ToDouble(reader["harga"].ToString());
obj.Remark = reader["remark"].ToString();
obj.Terjual = Convert.ToDouble(reader["terjual"].ToString());
obj.IsNew = false;
_products.Add(obj);
}
}
}
}
}
//untuk memanggil di form menggunakan
ProductList _objProduct = new ProductList();
_objProduct.getAllProduct();
//looping object
foreach (Product p in _objProduct.Products) {
}
sekian terima kasih. semoga bermanfaat.
//
yuhuu...bermanfaat banget gan
ReplyDeletePenjepit lcd