Tapi disini saya tidak akan menjelaskan secara detail dari awal sampai akhir, saya hanya akan memberikan event click yang dibutuhkan untuk melakukan CRUD database di WPF.
Pertama, event click untuk insert :
private void btnInsertSchedule_Click(object sender, RoutedEventArgs e){SqlConnection sconn = new SqlConnection("Data Source=AZIDA-PC\\ILKOMIPB;Initial Catalog=imk2011;Integrated Security=True");string a = DateTimePicker1.Text;string b = textBoxAuthor.Text;string c = textBoxSubject.Text;string d = textBoxInformation.Text;string f = category.Text;if ((a == "") || (b == "") || (c == "") || (d == "") || (f == "")){MessageBox.Show("All textBox are required!");}else{SqlDataAdapter sda = new SqlDataAdapter();sda.InsertCommand = new SqlCommand("INSERT INTO schedule VALUES(@dateTime,@author,@category,@subject,@information)", sconn);sda.InsertCommand.Parameters.Add("@dateTime", SqlDbType.DateTime).Value = DateTimePicker1.Text;sda.InsertCommand.Parameters.Add("@author", SqlDbType.VarChar).Value = textBoxAuthor.Text;sda.InsertCommand.Parameters.Add("@category", SqlDbType.VarChar).Value = category.Text;sda.InsertCommand.Parameters.Add("@subject", SqlDbType.VarChar).Value = textBoxSubject.Text;sda.InsertCommand.Parameters.Add("@information", SqlDbType.VarChar).Value = textBoxInformation.Text;MessageBox.Show("New schedule added successfully");sconn.Open();sda.InsertCommand.ExecuteNonQuery();sconn.Close();BindDataSchedule();DateTimePicker1.Text = "";textBoxAuthor.Text = "";textBoxSubject.Text = "";textBoxInformation.Text = "";}}
Kedua, event click untuk read :
public void btnTampil_Click(object sender, RoutedEventArgs e){SqlConnection sconn = new SqlConnection("Data Source=AZIDA-PC\\ILKOMIPB;Initial Catalog=imk2011;Integrated Security=True");SqlCommand scomm = new SqlCommand("SELECT dateTime,author,category,subject,information FROM schedule", sconn);DataSet ds = new DataSet();SqlDataAdapter sda = new SqlDataAdapter();try{sconn.Open();sda.SelectCommand = scomm;sda.Fill(ds, "schedule");//menampilkan data di datagriddataGrid1.DataContext = ds;}catch (Exception ex){MessageBox.Show(ex.ToString());}finally{sconn.Close();}}
Ketiga, event click untuk update :
private void btnUpdateSchedule_Click(object sender, RoutedEventArgs e){SqlConnection sconn = new SqlConnection("Data Source=AZIDA-PC\\ILKOMIPB;Initial Catalog=imk2011;Integrated Security=True");string a = textBoxDateTime.Text;string b = textBoxAuthor.Text;string c = textBoxSubject.Text;string d = textBoxInformation.Text;string f = category.Text;if ((a == "") || (b == "") || (c == "") || (d == "") || (f == "")){MessageBox.Show("All textBox are required!");}else{try{sconn.Open();string update = @"Update schedule Set dateTime='" + a + "', author='" + b + "', category='" + f + "',information='" + d + "' where subject='" + c + "'";SqlCommand scomm = new SqlCommand(update, sconn);scomm.ExecuteNonQuery();MessageBox.Show("Schedule updated successfully");sconn.Close();this.Close();}catch (Exception ex){MessageBox.Show(ex.ToString());}}}
dan untuk mengambil data dari datagrid :
DataRowView _DataView = dataGrid1.CurrentCell.Item as DataRowView;if (_DataView != null){//mengambil data dari datagrid dan menampilkan ke window jadwalJadwal jw = new Jadwal();jw.Show();jw.textBoxDateTime.Text = _DataView.Row[0].ToString();jw.textBoxAuthor.Text = _DataView.Row[1].ToString();jw.category.Text = _DataView.Row[2].ToString();jw.textBoxSubject.Text = _DataView.Row[3].ToString();jw.textBoxInformation.Text = _DataView.Row[4].ToString();}
keempat, event delete :
private void btnDeleteSchedule_Click(object sender, RoutedEventArgs e){DataRowView _DataView = dataGrid1.CurrentCell.Item as DataRowView;if (_DataView != null){string id = _DataView.Row[3].ToString();try{SqlConnection sconn = new SqlConnection("Data Source=AZIDA-PC\\ILKOMIPB;Initial Catalog=imk2011;Integrated Security=True");sconn.Open();string dell = @"Delete from schedule where subject='" + id + "'";SqlCommand scomm = new SqlCommand(dell, sconn);scomm.ExecuteNonQuery();MessageBox.Show("One Schedule Deleted");this.BindDataDairy();}catch (Exception ex){MessageBox.Show(ex.ToString());}}}
Terimakasih atas kunjungannya.. Semoga bermanfaat dan tunggu postingan-postingan selanjutnya..
No comments:
Post a Comment