-1

我有以下查询

select * from DATABASE.dbo.Rooms r
where not exists
    (select * from DATABASE.dbo.Reservation
        where RoomNo = r.RoomNo
        and DateStart <= 'Textbox2.text'
        and DateEnd >= 'Textbox1.text')

标签应显示所有可用房间及其详细信息。我如何在标签控件上显示表字段数据

4

1 回答 1

2

很难知道从哪里开始,这里......

首先,了解C#和SQL的区别。您的 SQL 语句没有意义。使用参数化查询。使用参数化查询使用参数化查询!

string query = "select * from DATABASE.dbo.Rooms r "+
               "where not exists "+
               "(select * from DATABASE.dbo.Reservation "+
               "          where RoomNo = r.RoomNo "+
               "          and DateStart <= @endDate "+
               "          and DateEnd >= @startDate)"

这是您的 SQL 查询。现在从 C# 调用它:

using (SqlCommand cmd = new SqlCommand(query, connection);
{
    // Assign parameters. I assume that you have DateTimePickers instead
    // of text boxes.
    cmd.Parameters.AddWithValue("@startDate", datePicker1.Date);           
    cmd.Parameters.AddWithValue("@endDate", datePicker2.Date);

    using (SqlDataReader reader = cmd.ExecuteReader())
    {
        // Read all data into string builder (field name must be changed)
        StringBuilder sb = new StringBuilder();
        while (reader.Read())
          sb.Append(reader["FieldName"].ToString());

        // Assign this to label
        label1.Text = sb.ToString();
    }
}
于 2013-02-11T15:29:39.973 回答