Tuesday, November 27, 2018

c# db access save,hapus,cari

namespace nilaiPrj
{
    public partial class Form1 : Form
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\syamsul\akhir.accdb");

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            con.Open();
            string sql = "SELECT * FROM nilai";
            OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
            DataSet ds = new DataSet();
            da.Fill(ds,"nilai");
            grid1.DataSource = ds.Tables["nilai"].DefaultView;
        }

        private void bsimpan_Click(object sender, EventArgs e)
        {
string sql = string.Format("insert into nilai values('{0}','{1}','{2}')", textBox1.Text, textBox2.Text, textBox3.Text);
            OleDbCommand cmd = new OleDbCommand(sql, con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Data berhasil disimpan");
            cmd.Dispose();
           
        string tampil = "SELECT * FROM nilai";
            OleDbDataAdapter da = new OleDbDataAdapter(tampil, con);
            DataSet ds = new DataSet();
            da.Fill(ds, "nilai");
            grid1.DataSource = ds.Tables["nilai"].DefaultView;
            grid1.Refresh();

        }

        private void bcari_Click(object sender, EventArgs e)
        {
            OleDbDataReader dt = null;
            string sql = string.Format("select * from nilai where nim = '{0}'", textBox1.Text);
            OleDbCommand cmd = new OleDbCommand(sql, con);

            dt = cmd.ExecuteReader();
            if (dt.Read())
            {
                textBox2.Text = Convert.ToString(dt["nilaiuts"]);
                textBox3.Text = Convert.ToString(dt["nilaiuas"]);
            }
            else
            { MessageBox.Show("dt td ada"); }

        }




        private void bhapus_Click(object sender, EventArgs e)
        {
            string sql = string.Format("delete * from nilai where nim='" + textBox1.Text + "'");
            OleDbCommand cmd = new OleDbCommand(sql, con);

            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Data Telah Dihapus");

        }

        private void bupdate_Click(object sender, EventArgs e)
        {
            string sql = string.Format("update nilai set nama=@name where nim=@nim",con);
            //con.Open();
           
            OleDbCommand cmd = new OleDbCommand(sql, con);
            cmd.Parameters.AddWithValue("@name", textBox2.Text);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Data berhasil diupdate");
            cmd.Dispose();
          
        }
    }
}

No comments:

Post a Comment

logika algo: deret hitung suku ke n

#include <conio.h> #include <iostream> #include <stdio> main(){  int n=6,i,bil;  for(i=1;i<=n;i++)  { ...