【发布时间】:2016-09-18 12:55:53
【问题描述】:
private void btnadd_Click(object sender, EventArgs e)
{
try
{
conn.Open();
string sql = ("Insert into tbl_books values NameOfBook = @book, Author =@author, Publisher=@publisher,YearPublished=@year,Category=@category,ISBN=@isbn");
MySqlCommand sda = new MySqlCommand(sql,conn);
sda.Parameters.AddWithValue("@book", txtbook.Text);
sda.Parameters.AddWithValue("@author", txtauthor.Text);
sda.Parameters.AddWithValue("@publisher", txtpublisher.Text);
sda.Parameters.AddWithValue("@year", txtyear.Text);
sda.Parameters.AddWithValue("@category", cmbcategory.Text);
sda.Parameters.AddWithValue("@isbn", txtisbn.Text);
sda.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Item has been added");
showlv("Select * from tbl_books", lvbooks);
}
catch (Exception)
{
MessageBox.Show("Cannot Add Item");
}
}
代码有什么问题?它继续进入 catch 块。
【问题讨论】:
-
写一个无用的 catch 块不是一个好习惯。如果你真的想要一个 catch 块,至少显示异常消息(IE: catch(Exception ex) { MessageBox.Show(ex.Message); } )并告诉我们消息文本跨度>
-
学习 SQL 比自己编写语法/结构更好
-
对不起,我只是在学习。谢谢!
标签: c# mysql parameters