0% found this document useful (0 votes)
6 views4 pages

Konek

Uploaded by

211210012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Konek

Uploaded by

211210012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace koneksicsharp
{
public partial class Form1 : Form
{
OleDbConnection KoneksiDB;
string Conn;
public Form1()
{
InitializeComponent();
Conn = "provider=microsoft.ACE.OLEDB.12.0;Data
Source=|datadirectory|/Penjualan.accdb";
KoneksiDB = new OleDbConnection(Conn);
KoneksiDB.Open();
}

private void button1_Click(object sender, EventArgs e)


{
try
{
KoneksiDB.Close();
MessageBox.Show("database terkoneksi", "terkoneksi",
MessageBoxButtons.OKCancel);

}
catch (OleDbException salah)
{
MessageBox.Show(salah.ToString());
}
}

private void Form1_Load(object sender, EventArgs e)


{

// Memanggil metode untuk mengisi ComboBox pada saat load form

private void tampildata_Click(object sender, EventArgs e)


{
try
{
string database = "select * from stok,harga";
OleDbConnection connew = new OleDbConnection(Conn);
connew.Open();
OleDbDataAdapter da = new OleDbDataAdapter(database, connew);
DataSet ds = new DataSet();
da.Fill(ds, "stok");
connew.Close();
dgtampildata.DataSource = ds.Tables["stok"].DefaultView;
}
catch (OleDbException salah)
{
MessageBox.Show(salah.ToString());
}
}

private void dgtampildata_CellContentClick(object sender,


DataGridViewCellEventArgs e)
{

private void button2_Click(object sender, EventArgs e)


{
try
{
int kdProduk = int.Parse(textBox1.Text);
int nmProduk = int.Parse(textBox2.Text);
int stok = int.Parse(textBox3.Text);
int satuan = int.Parse(textBox5.Text);
int harga = int.Parse(textBox6.Text);
int kadaluarsa = int.Parse(textBox7.Text);

string sql = String.Format("insert into harga (kd_produk,


nm_produk, stok, satuan) values ({0}, {1}, {2}, {3})", kdProduk, nmProduk, stok,
satuan);
string sqli = String.Format("insert into stok (kd_produk, harga,
kadaluarsa) values ({0}, {1}, {2})", kdProduk, harga, kadaluarsa);

OleDbConnection connew = new OleDbConnection(Conn);


connew.Open();
OleDbCommand cmd = new OleDbCommand(sql, connew);
cmd.ExecuteNonQuery();
OleDbCommand cmdl = new OleDbCommand(sqli, connew);
cmdl.ExecuteNonQuery();
connew.Close();
MessageBox.Show("data berhasil disimpan");
}
catch (Exception ex)
{
MessageBox.Show("data gagal disimpan : " + ex.Message);
}
}

private void button3_Click(object sender, EventArgs e)


{
try
{

string sql = string.Format("delete from harga WHERE kd_produk=" +


int.Parse(textBox4.Text) + "");
OleDbConnection connew = new OleDbConnection(Conn);
connew.Open();
OleDbCommand cmd = new OleDbCommand(sql, connew);
cmd.ExecuteNonQuery();
connew.Close();
MessageBox.Show("hapus data");
}
catch (OleDbException salah)
{
MessageBox.Show(salah.ToString());
}
}

private void button4_Click(object sender, EventArgs e)


{
try
{
int kdProduk = int.Parse(textBox1.Text);
int nmProduk = int.Parse(textBox2.Text);
int stok = int.Parse(textBox3.Text);
int satuan = int.Parse(textBox5.Text);
int harga = int.Parse(textBox6.Text);
int kadaluarsa = int.Parse(textBox7.Text);
string sql = string.Format("UPDATE harga SET nm_produk = " +
nmProduk + ", stok = " + stok + ", satuan = " + satuan + ", WHERE kd_produk = " +
textBox1.Text + "", Conn);
OleDbConnection connew = new OleDbConnection(Conn);
connew.Open();
OleDbCommand cmd = new OleDbCommand(sql, connew);
cmd.ExecuteNonQuery();
MessageBox.Show("data berhasil diupdate");
}
catch (Exception ex)
{
MessageBox.Show("data gagal diupdate : " + ex.Message);
}
}

private void bttampildata2_Click(object sender, EventArgs e)


{
string database = "select * from haha";
OleDbConnection connew = new OleDbConnection(Conn);
connew.Open();
OleDbDataAdapter da = new OleDbDataAdapter(database, connew);
DataSet ds = new DataSet();
da.Fill(ds, "haha");
connew.Close();
dgtampildata.DataSource = ds.Tables["haha"].DefaultView;
}

private void textBox1_TextChanged(object sender, EventArgs e)


{

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)


{
try
{
// Mengisi ComboBox1 dari tabel 'stok'
string query = "SELECT DISTINCT item FROM stok";
OleDbCommand cmd = new OleDbCommand(query, KoneksiDB);
OleDbDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
}

reader.Close();
}
catch (Exception ex)
{
MessageBox.Show("Terjadi kesalahan saat mengisi ComboBox: " +
ex.Message);
}
}
}
}

You might also like