Vous êtes sur la page 1sur 17

Buat database di sql server

CREATE DATABASE kantinAmikom

CREATE TABLE admin(


userId VARCHAR(10) NOT NULL PRIMARY KEY,
password VARCHAR(10) NOT NULL,
);

CREATE TABLE customer(


idCustomer VARCHAR(6) NOT NULL PRIMARY KEY,
namaCustomer VARCHAR(20) NOT NULL,
nominalIsi INT NOT NULL DEFAULT 0,
);

CREATE TABLE menu(


idMenu VARCHAR(5) NOT NULL PRIMARY KEY,
namaMenu VARCHAR(20) NOT NULL,
harga INT NOT NULL,
);

CREATE TABLE transaksi(


idTransaksi VARCHAR(10) NOT NULL PRIMARY KEY,
tglTransaksi CHAR(10) NOT NULL,
idCustomer VARCHAR(6) NOT NULL,
CONSTRAINT FK_CUSTOMER FOREIGN KEY (idCustomer) REFERENCES
customer(idCustomer)
);

CREATE TABLE detailTransaksi(


idTransaksi VARCHAR(10) NOT NULL,
idMenu VARCHAR(5) NOT NULL,
CONSTRAINT FK_TRANSAKSI FOREIGN KEY (idTransaksi) REFERENCES
transaksi(idTransaksi),
CONSTRAINT FK_MENU FOREIGN KEY (idMenu) REFERENCES menu(idMenu)
);
Praktikum 8
Listing program :

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.SqlClient;

namespace CUSTOMER
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

SqlConnection con = new SqlConnection


(@"Data Source=Akbar-PC;Initial Catalog=kantinAmikom;Integrated Security=True");

private void resetdata()


{
txtid.Text= "";
txtnama.Text= "";
txtvoucher.Text= "";
}

private void showdata()


{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from customer";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter (cmd);
da.Fill(ds, "Customer");
dgvcustomer.DataSource = ds;
dgvcustomer.DataMember = "customer";
dgvcustomer.ReadOnly= true;

private void Form1_Load(object sender, EventArgs e){

this.customerTableAdapter.Fill(this.kantinAmikomDataSet1.customer);
resetdata ();
showdata ();
}

private void btncari_Click(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand ();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from cutomer namacustomer like '%" +
txtcaridata.Text + "%";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter (cmd);
da.Fill(ds, "customer");
dgvcustomer.DataSource = ds;
dgvcustomer.DataMember = "customer";
dgvcustomer.ReadOnly = true;
}

private void btninsert_Click(object sender, EventArgs e)


{
if (txtid.Text== "" | txtnama.Text== "" | txtvoucher.Text== "")
{
MessageBox.Show("Semua data harus diisi","Peringatan");
goto berhenti;
}

int num;
bool isNum = int.TryParse(txtvoucher.Text.ToString(), out num);
if (!isNum)
{
MessageBox.Show("isi voucher harus angka","Peringatan");
goto berhenti;
}

con.Open();
SqlCommand cmd = new SqlCommand ();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into customer values ('" + txtid.Text + "','" +
txtnama.Text + "'," + int.Parse(txtvoucher.Text) + ")";
cmd.ExecuteNonQuery();
con.Close();
showdata();
resetdata();
berhenti:
;
}

private void btnupdate_Click(object sender, EventArgs e)


{
if (txtid.Text == "" | txtnama.Text== "" | txtvoucher.Text== "")
{
MessageBox.Show("Semua data Harus diisi","Peringatan");
goto berhenti;
}
int num;
bool isNum = int.TryParse(txtvoucher.Text.ToString(), out num);
if (!isNum)
{
MessageBox.Show("isi voucher harus angka","Peringatan");
goto berhenti;
}

con.Open();
SqlCommand cmd = new SqlCommand ();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update customer set namaCustomer = '" + txtid.Text + "',
nominalIsi=" +
txtvoucher.Text + "Where idcustomer = '" + txtid.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
showdata();
resetdata();

berhenti:
;
}

private void btndelete_Click(object sender, EventArgs e)


{
if (txtid.Text == "")
{
MessageBox.Show("isi id customer yang akan dihapus");
goto berhenti;
}

con.Open();
SqlCommand cmd = new SqlCommand ();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from customer where idcustomer = '" + txtid.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
showdata();
resetdata();

berhenti:
;
}

private void btnreset_Click(object sender, EventArgs e)


{
showdata();
}

}
}

Data menu
Lisiting program :

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.SqlClient;

namespace Data_Menu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

SqlConnection con = new SqlConnection


(@"Data Source=AKBAR-PC;Initial Catalog=kantinAmikom;Integrated Security=True");
DataTable dt = new DataTable();
BindingSource bS = new BindingSource();

private void resetdata()


{
txtid.Text = "";
txtmenu.Text = "";
txtharga.Text = "";
}

private void bindingdata()


{
dt.Clear();
txtid.DataBindings.Clear ();
txtmenu.DataBindings.Clear();
txtharga.DataBindings.Clear();
SqlDataAdapter da = new SqlDataAdapter ("select * from menu", con);
SqlCommandBuilder scb = new SqlCommandBuilder (da);
da.Fill(dt);
bS.DataSource= dt;
dgvmenu.DataSource= bS;
txtid.DataBindings.Add("Text", bS, "idMenu");
txtmenu.DataBindings.Add("Text", bS, "namaMenu");
txtharga.DataBindings.Add("Text", bS, "harga");
}

private void showdata()


{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from Menu";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter (cmd);
da.Fill(ds, "Menu");
dgvmenu.DataSource = ds;
dgvmenu.DataMember = "Menu";
dgvmenu.ReadOnly= true;
}

private void Form1_Load(object sender, EventArgs e)


{
showdata();
resetdata();
bindingdata();
}

private void search_Click(object sender, EventArgs e)


{
bS.Filter = "namaMenu like '%" + txtfilter.Text + "%'";
}

private void next_Click(object sender, EventArgs e)


{
bS.MoveNext();
}

private void previous_Click(object sender, EventArgs e)


{
bS.MovePrevious();
}

private void refresh_Click(object sender, EventArgs e)


{
showdata();
resetdata();
}

private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)


{
resetdata();
}

private void save_Click(object sender, EventArgs e)


{
if (txtid.Text == "" | txtmenu.Text == "" | txtharga.Text == "")
{
MessageBox.Show("Semua data harus diisi","Peringatan");
goto berhenti;
}

int num;
bool isNum = int.TryParse(txtharga.Text.ToString(), out num);
if (!isNum)
{
MessageBox.Show("isi harga harus angka","Peringatan");
goto berhenti;
}

con.Open();
SqlCommand cmd = new SqlCommand ();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into menu values ('" + txtid.Text + "','" +
txtmenu.Text + "'," + int.Parse(txtharga.Text) + ")";
cmd.ExecuteNonQuery();
con.Close();
showdata();
resetdata();
berhenti:
;
}

private void toolStripButton1_Click(object sender, EventArgs e)


{
resetdata();
}
}
}

Store procedure
Buat query procedure di sql server

create procedure ADDMENU

@id varchar (5), @nama varchar(40), @harga int


as
if exists(select * from menu where idMenu = @id)

update menu set namaMenu = @nama, harga = @harga


where idMenu = @id
else

insert into menu values(@id,@nama,@harga)

create procedure DELMENU

@id varchar (5)


as
delete from menu where idMenu = @id

buat Rancangan Menu form

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.SqlClient;

namespace Stored_Procedure
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

SqlConnection con = new SqlConnection


(@"Data Source=AKBAR-PC;Initial Catalog=kantinAmikom;Integrated Security=True");

private void showdata()


{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from menu";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter (cmd);
da.Fill(ds, "menu");
dgvmenu.DataSource = ds;
dgvmenu.DataMember = "menu";
dgvmenu.ReadOnly= true;
}

private void resetdata()


{
txtid.Text = "";
txtnama.Text = "";
txtharga.Text = "";
}

private void menuBindingNavigatorSaveItem_Click(object sender, EventArgs e)


{

private void Form1_Load(object sender, EventArgs e)


{
showdata();
resetdata();
}

private void btnsave_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = new SqlCommand ();
cmd.Connection = con;
cmd.CommandText= "ADDmenu";
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter idmenu = new SqlParameter ("@id", SqlDbType.VarChar);


SqlParameter namamenu = new SqlParameter ("@nama", SqlDbType.VarChar);
SqlParameter harga = new SqlParameter ("@harga", SqlDbType.Int);

idmenu.Value=txtid.Text;
namamenu.Value=txtnama.Text;
harga.Value=txtharga.Text;

cmd.Parameters.Add(idmenu);
cmd.Parameters.Add(namamenu);
cmd.Parameters.Add(harga);

cmd.ExecuteNonQuery();
con.Close();
showdata();
resetdata();
}

private void btndelete_Click(object sender, EventArgs e)


{
con.Open();
SqlCommand cmd = new SqlCommand ();
cmd.Connection = con;
cmd.CommandText= "DELmenu";
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter idmenu = new SqlParameter("@id", SqlDbType.VarChar);

idmenu.Value = txtid.Text;
cmd.Parameters.Add(idmenu);
cmd.ExecuteNonQuery();
con.Close();
showdata();
}
}
}

Data pengguna

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.SqlClient;

namespace Data_Pengguna
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

SqlConnection con = new SqlConnection


(@"Data Source=AKBAR-PC;Initial Catalog=kantinAmikom;Integrated Security=True");
private void resetdata()
{
txtuser.Text = "";
txtpwd.Text = "";
}

private void showdata()


{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from admin";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "admin");
dgvadmin.DataSource = ds;
dgvadmin.DataMember = "admin";
dgvadmin.ReadOnly = true;
}

private string CaesarCipher(string value, int shift)


{
string[] joinCipter = new string[200];
string joinText = "";
String[] wordArray = value.Split(' ');

try
{
for (int x = 0; x < wordArray.Length; x++)
{
char[] buffer = wordArray[x].ToCharArray();
for (int i = 0; i < buffer.Length; i++)
{
char letter = buffer[i];
letter = (char)(letter + shift);

if (letter > 'z')


{
letter = (char)(letter - 26);
}

if (letter > 'a')


{
letter = (char)(letter + 26);
}
buffer[i] = letter;
}
string HasilKonversi = new string(buffer);
joinCipter[x] = HasilKonversi;
}
joinText = String.Join(" ", joinCipter);
return joinText;
}

catch (Exception e)
{
MessageBox.Show(e.ToString());
return null;
}
}

private void Form1_Load(object sender, EventArgs e)


{
resetdata();
showdata();
}
private void btnsave_Click(object sender, EventArgs e)
{
if (txtuser.Text == "" | txtpwd.Text == "")
{
MessageBox.Show("Semua data harus diisi","Peringatan");
goto berhenti;
}
string tekscipher = null;
tekscipher = CaesarCipher (txtpwd.Text, 17);

con.Open();
SqlCommand cmd = new SqlCommand ();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into admin values ('" + txtuser.Text + "','" +
tekscipher + "')";
cmd.ExecuteNonQuery();
con.Close();
showdata();
resetdata();
berhenti:
;
}

private void txtpwd_KeyPress(object sender, KeyPressEventArgs e)


{
e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToLower());
}

private void btnupdate_Click(object sender, EventArgs e)


{
if (txtuser.Text == "" | txtpwd.Text == "")
{
MessageBox.Show("Semua data harus diisi","Peringatan");
goto berhenti;
}
string tekscipher = null;
tekscipher = CaesarCipher (txtpwd.Text, 17);

con.Open();
SqlCommand cmd = new SqlCommand ();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update into set password='" + tekscipher + "'where userid='"
+
txtuser.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
showdata();
resetdata();
berhenti:
;
}

private void btndelete_Click(object sender, EventArgs e)


{
if (txtuser.Text == "")
{
MessageBox.Show("Isi user ID yang akan dihapus");
goto berhenti;
}

con.Open();
SqlCommand cmd = new SqlCommand ();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from admin where userid='" + txtuser.Text + "'";
cmd.ExecuteNonQuery();
con.Close();
showdata();
resetdata();
berhenti:
;
}
}
}

Form login

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.SqlClient;

namespace form_login
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

SqlConnection con = new SqlConnection


(@"Data Source=Akbar-PC;Initial Catalog=kantinAmikom_0367;Integrated
Security=True");

private string CaesarCipher(string value, int shift)


{
string[] joinCipter = new string[200];
string joinText = "";
String[] wordArray = value.Split(' ');

try
{
for (int x = 0; x < wordArray.Length; x++)
{
char[] buffer = wordArray[x].ToCharArray();
for (int i = 0; i < buffer.Length; i++)
{
char letter = buffer[i];
letter = (char)(letter + shift);

if (letter > 'z')


{
letter = (char)(letter - 26);
}

if (letter > 'a')


{
letter = (char)(letter + 26);
}
buffer[i] = letter;
}
string HasilKonversi = new string(buffer);
joinCipter[x] = HasilKonversi;
}
joinText = String.Join(" ", joinCipter);
return joinText;
}

catch (Exception e)
{
MessageBox.Show(e.ToString());
return null;
}
}
private void Form1_Load(object sender, EventArgs e)
{

private void btnlogin_Click(object sender, EventArgs e)


{
if (txtuser.Text == "" | txtpwd.Text == "")
{
MessageBox.Show("Semua data harus diisi", "Peringatan");
goto berhenti;
}
string tekscipher = null;
tekscipher = CaesarCipher(txtpwd.Text, 17);

con.Close();
SqlCommand cmd = new SqlCommand("selest * from admin where userid-'" +
txtuser.Text + "' and password= '" + tekscipher + "'", con);
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
rd.Read();
Form1 frm_menu = new Form1();
frm_menu.Show();
}
else
{
MessageBox.Show("User id atau password tidak valid", "Peringatan");
txtuser.Text = "";
txtpwd.Text = "";
txtuser.Focus();
rd.Close();
}
berhenti:
;
}

private void txtpwd_KeyPress(object sender, KeyPressEventArgs e)


{
e.KeyChar = Convert.ToChar(e.KeyChar.ToString().ToLower());
}

private void txtuser_TextChanged(object sender, EventArgs e)


{

}
}
}

Tampilan menu utama

Listing program :

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;

namespace tampilan
{
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}

private void mncustomer_Click(object sender, EventArgs e)


{
form1 frm_customer = new form1();
frm_customer.Show();
}

private void form1_Load(object sender, EventArgs e)


{

}
}
}

Vous aimerez peut-être aussi