简体   繁体   中英

c# using OleDbDataReader to get value from Access database

I'm making a library system for a school project and I'm already at the Borrowing System. There's a column in my database where it says "Availability", basically it will say if the Book is "Borrowed", "Reserved", or "Available" and I'm trying to get the "Available" value from my access database using the OleDb package but it won't work, still.

private void btnBorrows_Click(object sender, EventArgs e)
    {
        string connectionString = @"Provider=Microsoft Office 12.0 Access Database Engine OLE DB Provider;" + @"Data source= C:\Users\Administrator\Desktop\dtbase\Database1.accdb";

        string queryString = "SELECT * FROM Books";
        string Av = string.Empty;
        try
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                OleDbCommand command = new OleDbCommand(queryString, connection);
                connection.Open();
                OleDbDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    Av = reader.["Availability"].ToString();
                }

                if (Av == "Available")
                {
                command.Connection = connection;
                command.CommandText = "insert into AuditTrail (MemberID, MemberName, BookID, BookTitle, DateBorrowed, ReturnDate, Status) values ('" + txtbxMId.Text + "', '" + txtbxMN.Text + "', '" + txtbxBookId.Text + "', '" + txtbxBookTitle.Text + "', '" + txtbxDateNow.Text + "', '" + txtbxReturn.Text + "', '" + txtbxStatus.Text + "')";

                command.ExecuteNonQuery();
                MessageBox.Show("Borrowed!");
                }
                else
                {
                    MessageBox.Show("This Book Is Already Borrowed.");
                }
                reader.Close();
                connection.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Failed to connect to data source" + ex);
        }

as you can see the first thing that came to my mind was the if else statement, I don't know if I'm using the package wrong because every time I run the program it always jumps at the else statement even though the value of the Availability is "Available".

My plan is that if the book is "Borrowed" or "Reserved" then the user cannot either "Borrow" or "Reserve" it but if it is "Available" then the user can borrow it. I actually tried the other way around

if (Av == "Borrowed" || Av == "Reserved")
   {
      MessageBox.Show("The Book is Already Borrowed.");
   }
else
   {
      //Do the thing
   }

but it will always say "The Books is already Borrowed"

I know this is old and I am sure you have resolved it by now, but... Can't say for certain without actually looking at it... but if you have multiple rows/ records I am thinking that it always returns as "Borrowed" because you do not have a filter or WHERE clause within your SELECT statement. Thus it will always display the first record.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM