WORK 15 ปรับปรุงข้อมูลในตาราง (Update Data)
Code : FormBooks.cs
DataGridView1CellContentDoubleClick :
{
String id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
//เพิ่มาใหม่ จาก work 13
String bookname = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
String author = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
String publishedYear = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
String shelf = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
String isbn = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
String bookname = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
String author = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
String publishedYear = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
String shelf = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
String isbn = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
this.textBoxID.Text = id;
//เพิ่มาใหม่ จาก work 13
this.textBoxBookName.Text = bookname;
this.textBoxAuthor.Text = author;
this.textBoxPublishedYear.Text = publishedYear;
this.textBoxShelf.Text = shelf;
this.textBoxISBN.Text = isbn;
}
this.textBoxBookName.Text = bookname;
this.textBoxAuthor.Text = author;
this.textBoxPublishedYear.Text = publishedYear;
this.textBoxShelf.Text = shelf;
this.textBoxISBN.Text = isbn;
}
ButtonUpdateClick :
{
String dbName = "kvcLib.ldb";
String dataSource = "data source= " + dbName;
SQLiteConnection con = new SQLiteConnection(dataSource);
SQLiteCommand com = new SQLiteCommand(dataSource, con);
String bookName = textBoxBookName.Text;
String author = textBoxAuthor.Text;
int publishedYear = Int16.Parse(textBoxPublishedYear.Text);
String shelf = textBoxShelf.Text;
String isbn = textBoxISBN.Text;
String qry = "UPDATE books " +
"SET BookName = '" + bookName + "' , " +
"Author = '" + author + "', " +
"Published_year = " + publishedYear + ", " +
"Shelf = '" + shelf + "', "+
"ISBN = '" + isbn + "'" +
"WHERE ID = " + this.textBoxID.Text + ";";
con.Open();
com.CommandText = qry;
try {
com.ExecuteNonQuery();
MessageBox.Show("ปรับปรุงข้อมูลสำเร็จ");
} catch (Exception ex) {
MessageBox.Show("มีความผิดพลาด ปรับปรุงข้อมูลไม่สำเร็จ\n" + ex.ToString());
}
con.Close();
}
String dataSource = "data source= " + dbName;
SQLiteConnection con = new SQLiteConnection(dataSource);
SQLiteCommand com = new SQLiteCommand(dataSource, con);
String bookName = textBoxBookName.Text;
String author = textBoxAuthor.Text;
int publishedYear = Int16.Parse(textBoxPublishedYear.Text);
String shelf = textBoxShelf.Text;
String isbn = textBoxISBN.Text;
String qry = "UPDATE books " +
"SET BookName = '" + bookName + "' , " +
"Author = '" + author + "', " +
"Published_year = " + publishedYear + ", " +
"Shelf = '" + shelf + "', "+
"ISBN = '" + isbn + "'" +
"WHERE ID = " + this.textBoxID.Text + ";";
con.Open();
com.CommandText = qry;
try {
com.ExecuteNonQuery();
MessageBox.Show("ปรับปรุงข้อมูลสำเร็จ");
} catch (Exception ex) {
MessageBox.Show("มีความผิดพลาด ปรับปรุงข้อมูลไม่สำเร็จ\n" + ex.ToString());
}
con.Close();
}
0 ความคิดเห็น