Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将SQL数据获取到Label而不是DataGrid中_C#_Asp.net_Sql_Label - Fatal编程技术网

C# 将SQL数据获取到Label而不是DataGrid中

C# 将SQL数据获取到Label而不是DataGrid中,c#,asp.net,sql,label,C#,Asp.net,Sql,Label,我有以下疑问 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') 标签应显示所有可用房间及其详细信息。如何在标签控件上显

我有以下疑问

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')

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

很难知道从何处开始,这里

首先,了解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)"
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();
    }
}