在发布一本书之前,检查SQL中的Book_Availability(BookAvailability-1)

时间:2018-07-21 20:53:55

标签: c# sql-server database-connection

如果我将“ if,foreach和else语句置于注释//下”,则程序将运行并从SQL数据库中将帐面数量减少1。但是我要检查是否至少有1本可以提供。如果我这样保留,此代码将继续在“其他”语句中显示该消息。快速需要帮助,这是我的最终项目,需要在23.07之前完成。 :(

            int book_qty = 0;
            SqlCommand cmd2 = connection.CreateCommand();
            cmd2.CommandType = CommandType.Text;
            cmd2.CommandText = "SELECT * FROM Book_list WHERE BookName = '" + TextBoxBookName + "'";
            cmd2.ExecuteNonQuery();
            DataTable dt2 = new DataTable();
            SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
            da2.Fill(dt2);


            foreach (DataRow dr2 in dt2.Rows)
            {
                book_qty = Convert.ToInt32(dr2["book_qty"].ToString());
            }

            if (book_qty > 0)
            {



                SqlCommand cmd = connection.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO Issue_book VALUES(" + TextBoxSearchMembers.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("successful issue");
                this.Close();


            else
            {
                    MessageBox.Show("Book not available");
            }

2 个答案:

答案 0 :(得分:1)

您只从结果集中的最后一行检查book_qty,而不是所有行的BookAvailability。您可能想要做类似的事情:

            SqlCommand cmd2 = connection.CreateCommand();
            cmd2.CommandType = CommandType.Text;
            cmd2.CommandText = "SELECT BookAvailability FROM Book_list WHERE BookName = '" + TextBoxBookName + "'";
            var result = cmd2.ExecuteScalar();
            book_qty = Convert.ToInt32(result);

您需要确保只有一本书具有给定的书名。

在这种情况下,只需更正代码中的这一行也会有所帮助:

            book_qty = Convert.ToInt32(dr2["book_qty"].ToString());

            book_qty = Convert.ToInt32(dr2["BookAvailability"].ToString());

否则,您需要查询SUM(BookAvailability),但是以下代码将同时减少多本书的图书数量,这不是很好。

答案 1 :(得分:0)

未经测试的代码。我没有您的数据库。符合注释和解释。

private void OPCode()
        {
            try
            {
                //keep your connections close to the vest (local)
                using (SqlConnection connection = new SqlConnection())
                //a using block ensures that your objects are closed and disposed 
                //even if there is an error
                {
                    using (SqlCommand cmd2 = new SqlCommand("SELECT BookAvailability  FROM Book_list WHERE BookName = @BookName", connection))
                    {
                        //Always use parameters to protect from sql injection
                        //Also it is easier than fooling with the single quotes etc.
                        //If you are referring to a TextBox you need to provide what property is
                        //being accessed. I am not in a WPF right now and not sure if .Text
                        //is correct; may be .Content
                        //You need to check your database for correct data type and field size
                        cmd2.Parameters.Add("@BookName", SqlDbType.VarChar, 100).Value = TextBoxBookName.Text;
                        //A select statement is not a non-query
                        //You don't appear to be using the data table or data adapter
                        //so dump them extra objects just slow things dowm
                        connection.Open();
                       //Comment out the next 2 lines and replaced with
                       //Edit Update
                        //var returnVal = cmd2.ExecuteScalar() ?? 0;
                        //if ((int)returnVal > 0)




               //*************************************************************
                            //Edit Update
                            //*************************************************************
                            //in case the query returns a null, normally an integer cannot
                            //hold the value of null so we use nullable types
                            // the (int?) casts the result of the query to Nullable of int
                            Nullable<int> returnVal = (int?)cmd2.ExecuteScalar();
                            //now we can use the .GetValueOrDefault to return the value
                            //if it is not null of the default value of the int (Which is 0)
                            int bookCount = returnVal.GetValueOrDefault();
                            //at this point bookCount should be a real int - no cast necessary
                            if (bookCount > 0)

 //**************************************************************
                           //End Edit Update
                           //**************************************************************
                                {
                                    using (SqlCommand cmd = new SqlCommand("INSERT INTO issue_book VALUES(@SearchMembers etc", connection))
                                    {
                                        //set up the parameters for this command just like the sample above
                                        cmd.Parameters.Add("@SearchMembers", SqlDbType.VarChar, 100).Value = TextBoxSearchMembers.Text;
                                        cmd.ExecuteNonQuery();
                                    }
                                    using (SqlCommand cmd1 = new SqlCommand("UPDATE Book_list SET BookAvailability = BookAvailability-1 WHERE BookName = @BoxBookName;", connection))
                                    {
                                        cmd1.Parameters.Add("@BoxBookName", SqlDbType.VarChar, 100);
                                        cmd1.ExecuteNonQuery();
                                    }
                                    MessageBox.Show("success");
                                    this.Close();
                                }
                                else
                                {
                                    MessageBox.Show("Book not available");
                                }
                            }
                        }
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.ToString());
                    }
                }