因此,我正在建立一个图书馆信息系统,但偶然发现了一个问题。当我想把书交给图书馆会员时,程序会检查数据库中是否至少有1本书,如果有-它会在数据库中减去,因此可用的书是-1。但是,当我尝试退还该书时,该书是从某个成员那里拿走的,所以一切正常,但是它不会向数据库中的可用书加+1。
送书代码:
try
{
using (SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-7PMP6IR\SQLEXPRESS;Initial Catalog=BibliotekaDb;Integrated Security=True"))
{
using (SqlCommand cmd2 = new SqlCommand("SELECT BookAvailability FROM Book_list WHERE BookName = @BookName", con))
{
cmd2.Parameters.Add("@BookName", SqlDbType.VarChar, 100).Value = TextBoxBookName.Text;
con.Open();
var returnVal = cmd2.ExecuteScalar() ?? 0;
if ((int)returnVal > 0)
{
SqlCommand cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO book_issue VALUES(" + TextBoxSreachMembers.Text + ",'" + TextBoxMemberName.Text + "','" + TextBoxMemberContact.Text + "','" + TextBoxMemberEmail.Text + "','" + TextBoxBookName.Text + "', '" + DateTimePicker1.Text + "', '')";
cmd.ExecuteNonQuery();
SqlCommand cmd1 = connection.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "UPDATE Book_list SET BookAvailability = BookAvailability-1 WHERE BookName ='" + TextBoxBookName.Text + "'";
cmd1.ExecuteNonQuery();
MessageBox.Show("book issued");
this.Close();
}
else
{
MessageBox.Show("no books available");
}
}
}
}
catch (Exception exc)
{
MessageBox.Show("no books available");
//MessageBox.Show(exc.ToString());
}
退还书的代码:
SqlCommand cmd = connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE book_issue SET DateReturnBook ='" + DatePickerReturn.ToString() + "', BookName ='" + TextBoxBookName.Text + "'";
cmd.ExecuteNonQuery();
SqlCommand cmd1 = connection.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "UPDATE Book_list SET BookAvailability = BookAvailability+1 WHERE BookName ='" + TextBoxBookName.Text + "'";
cmd1.ExecuteNonQuery();
MessageBox.Show("book returned !");
this.Close();
答案 0 :(得分:0)
错误是一个错字,我正在从textboxmember name中插入值@BookName
答案 1 :(得分:0)
我认为这行代码是错误的
cmd.CommandText = "UPDATE book_issue SET DateReturnBook ='" + DatePickerReturn.ToString() + "', BookName ='" + TextBoxBookName.Text + "'";
我认为必须仅更新这本书的记录
cmd.CommandText = "UPDATE book_issue SET DateReturnBook ='" + DatePickerReturn.ToString() + "' where BookName ='" + TextBoxBookName.Text + "'";
因为首先将更新表 book_issue 中的所有记录,另外您还必须添加其他条件以仅更新借阅成员的记录而不是该书的所有记录