很难知道从哪里开始,这里......
首先,了解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();
}
}