Info:
create table buku (
id_buku int IDENTITY(1,1) primary key,
nama_buku varchar null,
pengarang varchar(100),
);
C#
Pengambilan alamat server /database yang ada di dalam variable konf di ambil dari koneksi sql
server yang ada di visual
Menampilkan data
1. Buat halaman form 1 tambahkan label, textbox, buton dan datagridview sesuai dengan jumlah
kolom yang ada di user.
2. Setelah selesai membuat form, pindah ke Model.cs atau ke kelas Model , silakan masukan
perintah tersebut di bawah public Model
public SqlCommand run(string q)
{
SqlCommand state =null;
try
{
kon.Open();
query = new SqlCommand();
query.Connection = kon;
query.CommandType = CommandType.Text;
query.CommandText = q;
state = query;
}
catch (Exception e)
{
MessageBox.Show("gagal koneksi");
}
return state;
}
//parsing data
public string parsing(string[] record)
{
string query;
string re = "";
int no = 1;
string tanda = "";
foreach (string row in record)
{
if (no < record.Count())
{
tanda = ",";
}
else
{
tanda = " ";
}
re += "'" + row + "'" + tanda;
no++;
}
query = re;
return query;
}
Seperti gambar di bawah
2. Kembali ke halam form lalu klik pada tombol simpan dan masukan perintah di bawah
public void simpan()
{
string table = "users";
string column = "id_user,nama,alamat,username,password";
string[] record = new string[] { textBox1.Text, textBox2.Text,
textBox3.Text , textBox4.Text,textBox5.Text };
Model query = new Model();
if (query.simpan(table, column, record))
{
MessageBox.Show(" data berhasil di ubah");
}
else
{
MessageBox.Show("data gagal di simpan");
}
}
1. Di halaman form 1, klik kanan pada grid view dan pilih editcolumns
2. Setelah muncul dialog edit columns, klik ada yang ada di bawah
Untuk name isi dengan Edit, typenya buttoncolumn dan header text Edit, dan buat lagi untuk
hapusnya juga .
4. Ketika sudah selesai membuat button di dalam grid, klik double pada grid tersebut lalu masukan
perintah ini di
try
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
if (e.ColumnIndex == this.Edit.Index && e.RowIndex >= -1)
{
textBox1.Text = row.Cells[2].Value.ToString();
textBox2.Text = row.Cells[3].Value.ToString();
textBox3.Text = row.Cells[4].Value.ToString();
textBox4.Text = row.Cells[5].Value.ToString();
textBox5.Text = row.Cells[6].Value.ToString();
}
else if (e.ColumnIndex == this.Delete.Index && e.RowIndex >= -1)
{
Model konek = new Model();
string id = row.Cells[2].Value.ToString();
string hapus = "delete from pengguna where kode_pengguna='"
+ id + "'";
if (result == DialogResult.Yes)
{
delete(id);
}
tampil();
}
}
catch (Exception ex)
{
}
5. Tambah method baru untuk menghapus di halaman form1 dan masukan perintah di bawah
public void delete( string id)
{
string table = "users";
string search = "id_user ='" + id + "'";
//delete data
public bool delete(string table, string search)
{
bool stat = false;
string sql = "delete FROM " + table + " WHERE " + search;
run(sql).ExecuteNonQuery();
kon.Close();
stat = true;
return stat;
}
//update data
public bool update(string table, string record, string search)
{
bool stat = false;
string sql = "update " + table + " set " + record + " WHERE
"+search;
run(sql).ExecuteNonQuery();
kon.Close();
stat = true;
return stat;
}
//pencarian data
public bool search(string table, string column, string find)
{
bool d = false;
string sql = "SELECT " + column + " FROM " + table + " WHERE " +
find;
SqlDataReader rd = run(sql).ExecuteReader();
if (rd.Read())
{
kon.Close();
d = true;
}
else
{
kon.Close();
d = false;
}
return d;
}
3. Klik double pada tombol simpan, hapus perintah simpan(); dan tampil(); ganti dengan perintah
di bawah
string table = "users"; // nama table yang akan di simpan
string find = "id_user ='" + textBox1.Text + "'"; // field dan
record yang ingin di cari
string column = "*"; // kolom yang ingin di tampilkan
Model query = new Model(); //membuka file Model
//update(); // update
if(query.search(table, column, find) == true)
{
update();
}
else
{
simpan();
}
tampil(); // menampilkan data