需要算法进行房间预订

时间:2019-04-18 11:43:09

标签: asp.net web-applications

我需要一种房间预定算法,即一旦预定房间,就不应在该日期之前预定房间。我正在使用sql server,并且正在使用状态为活动或已预定,但无法正常工作。

public void res(DateTime? arrivalDate,DateTime? departureDate)
    {

        SqlConnection myConn = new SqlConnection();
        myConn.ConnectionString = (@"Server=DESKTOP-8JJO9NL\SQLEXPRESS;Database=HMS;Trusted_Connection=True");
        myConn.Open();
        SqlCommand com = new SqlCommand();
        com.Connection = myConn;
        string sql = "";
        sql = "select count(*) from Roomsdata where Room#=@roomnum and Status='Active and arrivaldate >= @arrivalDate and departure <= @departureDate'";
        com.CommandText = sql;
        com.Parameters.Clear();
        com.Parameters.AddWithValue("@roomnum", Roomnum);
        com.Parameters.AddWithValue("@arrivalDate", arrivalDate);
        com.Parameters.AddWithValue("@departureDate", departureDate);

        int numRecords = (int)com.ExecuteScalar();

        if (numRecords == 1)
        {

                SqlConnection myConn2 = new SqlConnection();
                myConn2.ConnectionString = (@"Server=DESKTOP-8JJO9NL\SQLEXPRESS;Database=HMS;Trusted_Connection=True");
                myConn2.Open();

                SqlCommand myCmd = new SqlCommand("update Roomsdata set [customername]=@customername ,[contact] =@contact  ,[arrivaldate]=@arrivaldate,[departure]=@departure  ,[Status]='Booked' ,[stayingdays]=DATEDIFF(day,arrivaldate,departure) ,[Total amount]=DATEDIFF(day,arrivaldate,departure)*1278 FROM [HMS].[dbo].[Roomsdata] where Room#=@roomnum1", myConn2);
                myCmd.Parameters.AddWithValue("roomnum1", Roomnum);
                myCmd.Parameters.AddWithValue("@customername", Costumername);
                myCmd.Parameters.AddWithValue("@contact", Contact);
                myCmd.Parameters.AddWithValue("@arrivaldate", Arrivaldate);
                myCmd.Parameters.AddWithValue("@departure", Departuredate);
                myCmd.ExecuteNonQuery();
                myConn2.Close();

           }
        else
        {
            throw new Exception("Room is Already Booked");
        }

0 个答案:

没有答案