Minggu, 28 Mei 2017

Kode Program Membuat Aplikasi Database Satu Tabel Dengan Bahasa Pemrograman Visual C#

Kode Program Membuat Aplikasi Database Satu Tabel Dengan Bahasa Pemrograman Visual C#

Visual C #


-          Form Barang (saat didesign)
 




-       Form Barang (saat Running/ dijalankan)






Berikut listing/ kodenya untuk Form barang (database) dengan C# :

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;
using System.IO;


namespace WindowsFormsApplicationAcspttc_sharp_
{
   
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
          
        }
Form                                                                                                                                       Load
        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'acspttDataSet1.brgptt' table. You can move, or remove it, as needed.
            this.brgpttTableAdapter.Fill(this.acspttDataSet1.brgptt);
          
        }
Button First                                                                                                                            click
        private void Button1_Click(object sender, EventArgs e)
        {
            this.brgpttBindingSource.MoveFirst();
        }
Button Prev                                                                                                                            click
        private void Button2_Click(object sender, EventArgs e)
        {
            this.brgpttBindingSource.MovePrevious();
        }
Button Next                                                                                                                           click
        private void Button3_Click(object sender, EventArgs e)
        {
            this.brgpttBindingSource.MoveNext();           
        }
Button Last                                                                                                                            click
        private void Button4_Click(object sender, EventArgs e)
        {
            this.brgpttBindingSource.MoveLast();
        }
Button Add                                                                                                                             click
        private void Button5_Click(object sender, EventArgs e)
        {
            this.Button6.Enabled= (true);
            this.Button5.Enabled = (false);

            this.TextBox1.Text = "";
            this.TextBox2.Text = "";
            this.TextBox3.Text = "";
            this.TextBox4.Text = "";
            this.TextBox6.Text = "";
            this.TextBox7.Text = "";

            this.TextBox1.Focus();

            this.Button1.Enabled = (false);
            this.Button2.Enabled = (false);
            this.Button3.Enabled = (false);
            this.Button4.Enabled = (false);

            this.Button7.Enabled = (false);
            this.Button8.Enabled = (false);
            this.Button10.Enabled = (false);
            this.Button11.Enabled = (false);
            this.Button12.Enabled = (false);

            this.ListBox1.Enabled = (false);
            this.DataGridView1.Enabled = (false);
        }
Button Save                                                                                                                           click
        private void Button6_Click(object sender, EventArgs e)
        {
            this.Button6.Enabled = (false);
        this.Button5.Enabled = (true);

        if (this.TextBox1.Text != ""){
            try {
                OleDbConnection cnn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='D:/Data Agus/Acsptt.accdb'");
              
                cnn.Open();

                String mm1 = TextBox1.Text;
                String mm2 = TextBox2.Text;
                String mm3 = TextBox3.Text;
                if (this.TextBox4.Text == ""){
                    this.TextBox4.Text = "D:/Data Agus/kosong.bmp";
                   
                }
                String mm4 = TextBox4.Text;
                String mm6 = TextBox6.Text;
                String mm7 = TextBox7.Text;

                int bufferLen = 600000;
                 FileStream fs = new FileStream(mm4, FileMode.Open, FileAccess.Read);
                 BinaryReader br = new BinaryReader(fs);                
                 byte[] mbytes = br.ReadBytes(bufferLen);
                             

                String strselect = "insert into brgptt(kbrgptt,nmbrgptt,typebrgptt,pictfilebrgptt,pictbrgptt,vabrgptt,pictfilewebbrgptt)values('" + mm1 + "','" + mm2 + "','" + mm3 + "','" + mm4 + "',@ss,'" + mm6 + "','" + mm7 + "'  )";
                OleDbCommand sqlku = new OleDbCommand(strselect, cnn);
                sqlku.Parameters.Add("@ss", OleDbType.Binary, mbytes.Length).Value = mbytes;
                sqlku.ExecuteNonQuery();

                cnn.Close();

                this.brgpttTableAdapter.Fill(this.acspttDataSet1.brgptt);

                this.Button1.Enabled = (true);
                this.Button2.Enabled = (true);
                this.Button3.Enabled = (true);
                this.Button4.Enabled = (true);

                this.Button7.Enabled = (true);
                this.Button8.Enabled = (true);
                this.Button10.Enabled = (true);
                this.Button11.Enabled = (true);
                this.Button12.Enabled = (true);

                this.ListBox1.Enabled = (true);
                this.DataGridView1.Enabled = (true);
            }
       
            catch (Exception ex){

                MessageBox.Show("Error"+ex+"", "Saving.......",
MessageBoxButtons.OK, MessageBoxIcon.Information);

             
                this.TextBox4.Focus();

                this.Button6.Enabled = (true);
                this.Button5.Enabled = (false);
           
        }}
         else
            {
                MessageBox.Show("Error...Kode Barangnya belum ada (masih kosong)..!", "Saving.......",
    MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.TextBox1.Focus();
          
            this.Button6.Enabled = (true);
            this.Button5.Enabled = (false);

       
            }
        }


Button Edit                                                                                                                             click
        private void Button7_Click(object sender, EventArgs e)
        {
           
           if ( MessageBox.Show("Apakah Data ini yakin akan diedit", "Perhatian",
            MessageBoxButtons.YesNo, MessageBoxIcon.Information)==DialogResult.Yes)
           {
            try {
                OleDbConnection cnn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='D:/Data Agus/Acsptt.accdb'");
              
                cnn.Open();

                String mm1 = TextBox1.Text;
                String mm2 = TextBox2.Text;
                String mm3 = TextBox3.Text;
                String mm4 = TextBox4.Text;
                String mm6 = TextBox6.Text;
                String mm7 = TextBox7.Text;

                String mm1k = TextBox5.Text;

                int bufferLen = 600000;

                 FileStream fs = new FileStream(mm4, FileMode.Open, FileAccess.Read);
                 BinaryReader br = new BinaryReader(fs);
                
                 byte[] mbytes = br.ReadBytes(bufferLen);

             
                String strselect = "update  brgptt set kbrgptt='" + mm1 + "',nmbrgptt='" + mm2 + "',typebrgptt='" + mm3 + "',pictfilebrgptt ='" + mm4 + "',pictbrgptt =@ss,vabrgptt='" + mm6 + "',pictfilewebbrgptt ='" + mm7 + "'  where kbrgptt='" + mm1k + "'     ";
                OleDbCommand sqlku = new OleDbCommand(strselect, cnn);
                sqlku.Parameters.Add("@ss", OleDbType.Binary, mbytes.Length).Value = mbytes;
                sqlku.ExecuteNonQuery();

                cnn.Close();

                this.brgpttTableAdapter.Fill(this.acspttDataSet1.brgptt);

             }
       
            catch (Exception ex){
                MessageBox.Show("Error" + ex + "", "Saving.......",
MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            }
        else{
            this.brgpttTableAdapter.Fill(this.acspttDataSet1.brgptt);
            this.TextBox5.Focus();
        }
        }
Button Delete                                                                                                                                    click
        private void Button8_Click(object sender, EventArgs e)
        {
            OleDbConnection cnn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='D:/Data Agus/Acsptt.accdb'");

            cnn.Open();


            String mm5 = TextBox5.Text;



            String strselect = "delete from brgptt where kbrgptt='" + mm5 + "'";
            OleDbCommand sqlku = new OleDbCommand(strselect, cnn);

            sqlku.ExecuteNonQuery();

            cnn.Close();

            this.brgpttTableAdapter.Fill(this.acspttDataSet1.brgptt);
            this.DataGridView1.Refresh();

        }
Button Exit                                                                                                                             click
        private void Button9_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
Button Find                                                                                                                            click
        private void Button10_Click(object sender, EventArgs e)
        {
             this.ListBox1.Text = this.TextBox5.Text;
             this.ListBox1.Refresh();
        }
Button End Find                                                                                                                     click
        private void Button11_Click(object sender, EventArgs e)
        {
            this.TextBox5.Text = "";
        }
Button Report                                                                                                                                    click
        private void Button12_Click(object sender, EventArgs e)
        {
            FormReport1 r1 = new FormReport1();
            r1.Show();
        }
Button View Gambar                                                                                                             click
        private void Button13_Click(object sender, EventArgs e)
        {
            if (this.TextBox4.Text == "") {
            MessageBox.Show("File data gambarnya belum ada atau belum ditulis", "Saving.......",
    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
                else
            {

            try {
              
                this.PictureBox1.Image = Image.FromFile(TextBox4.Text);
                this.PictureBox1.Refresh();
            }
            catch (Exception ex){
                MessageBox.Show("File data gambarnya tidak ada ", "Saving.......",
      MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

                }
        }
Button Cancel Add/ Save                                                                                                      click
        private void Button14_Click(object sender, EventArgs e)
        {
            this.Button6.Enabled = (false);
            this.Button5.Enabled = (true);

            this.TextBox1.Text = "";
            this.TextBox2.Text = "";
            this.TextBox3.Text = "";
            this.TextBox4.Text = "";
            this.TextBox6.Text = "";
            this.TextBox7.Text = "";

            this.TextBox1.Focus();

            this.brgpttTableAdapter.Fill(this.acspttDataSet1.brgptt);
        
            this.Button1.Enabled = (true);
            this.Button2.Enabled = (true);
            this.Button3.Enabled = (true);
            this.Button4.Enabled = (true);

            this.Button7.Enabled = (true);
            this.Button8.Enabled = (true);
            this.Button10.Enabled = (true);
            this.Button11.Enabled = (true);
            this.Button12.Enabled = (true);

            this.ListBox1.Enabled = (true);
            this.DataGridView1.Enabled = (true);
        }
    }
}





Tidak ada komentar:

Posting Komentar