C#布尔行每次都用空列计数

时间:2018-08-12 15:58:59

标签: c#

我在book1表中有2列book2Borrowes到,但是有些时候用户借用了这本书,有些时候没有,所以我需要删除没有借用任何书的用户( book1book2)。我得到了每一次True的价值。让我知道我能做什么谢谢。

我尝试过了...

 con.Open();

 bool readerHasRows = false;
 string brid = txtBr_id.Text;
 String syntax = "SELECT book1, book2 FROM Borrowes WHERE brId = @brid";

 using (SqlCommand cmd = new SqlCommand(syntax, con))
 {
     cmd.Parameters.AddWithValue("@brid", txtBr_id.Text);


     using (SqlDataReader reader = cmd.ExecuteReader())
     {
        readerHasRows = (!reader[0].IsDBNull);
     }
 }
 con.Close();

 if (readerHasRows == true)
 {
    MessageBox.Show("This borrower has borrowed the book please collect the book first.");
 }

1 个答案:

答案 0 :(得分:0)

如果您将没有借书的用户的book1和book2设置为null:

 con.Open();

 bool readerHasRows = false;
 string brid = txtBr_id.Text;
 String syntax = "SELECT book1, book2 FROM Borrowes WHERE brId = @brid AND (book1 IS NOT NULL OR book2 IS NOT NULL)";

 using (SqlCommand cmd = new SqlCommand(syntax, con))
 {
     cmd.Parameters.AddWithValue("@brid", txtBr_id.Text);


     using (SqlDataReader reader = cmd.ExecuteReader())
     {
        readerHasRows = reader.read();
     }
 }
 con.Close();

 if (readerHasRows == true)
 {
    MessageBox.Show("This borrower has borrowed the book please collect the book first.");
 }