using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace DataAccessLayer
{
public class ConnectionDb
{
/*PLEASE DO NOT REMOVE/MODIFY THE VARIABLES and METHODS - InsertData and
GetAllData LISTED BELOW/
/Internally used method starts*/
public static SqlConnection sqlConnection = null;
public static SqlCommand sqlCommand = null;
public static SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
public static DataSet dataset = new DataSet();
public static DataTable dataTable;
static readonly string connection =
ConfigurationManager.ConnectionStrings["HotelManagement"].ConnectionString;
public static int InsertData()
{
using (sqlConnection = new SqlConnection(connection))
{
sqlConnection.Open();
StringBuilder commandBuilder = new StringBuilder();
commandBuilder.Append("if not exists(select 1 from reservation
where iRoomNo=101 and dCheckInDate='2010-03-02' and dCheckoutDate='2017-03-04') ");
commandBuilder.Append("begin insert into reservation
values(134,'03/02/2010','03/04/2017',101,'Booked',6000) end");
SqlCommand cmd = new SqlCommand(commandBuilder.ToString())
{
Connection = sqlConnection
};
return cmd.ExecuteNonQuery();
}
}
public static DataTable GetAllData()
{
using (sqlConnection = new SqlConnection(connection))
{
sqlConnection.Open();
sqlCommand = new SqlCommand("select * from Reservation",
sqlConnection);
sqlDataAdapter = new SqlDataAdapter(sqlCommand);
dataset = new DataSet();
sqlDataAdapter.Fill(dataset);
dataTable = dataset.Tables[0];
return dataTable;
}
}
/*Internally used method end*/
public DataTable GetRoomTypes()
{
try
{
sqlConnection = new SqlConnection(connection);
sqlConnection.Open();
sqlCommand = new SqlCommand("select * from Roomtype",
sqlConnection);
sqlDataAdapter = new SqlDataAdapter(sqlCommand);
dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally { sqlConnection.Close(); }
return dataTable;
}
public int AddBooking(int cust_id, DateTime check_in, DateTime check_out,
int room_no, string reservation_Status, float total_Charge)
{
int reservationID = 0;
int res;
try
{
if (ValidateBooking(check_in, room_no) == false)
{
sqlConnection = new SqlConnection(connection);
sqlConnection.Open();
sqlCommand = new SqlCommand("insert into Reservation
values(@p1,@p2,@p3,@p4,@p5,@p6)", sqlConnection);
sqlCommand.Parameters.AddWithValue("@p1", cust_id);
sqlCommand.Parameters.AddWithValue("@p2", check_in);
sqlCommand.Parameters.AddWithValue("@p3", check_out);
sqlCommand.Parameters.AddWithValue("@p4", room_no);
sqlCommand.Parameters.AddWithValue("@p5", reservation_Status);
sqlCommand.Parameters.AddWithValue("@p6", total_Charge);
res = sqlCommand.ExecuteNonQuery();
reservationID++;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
sqlConnection.Close();
}
return reservationID;
}
public static bool ValidateBooking(DateTime checkInDate, int roomNo)
{
using (sqlConnection = new SqlConnection(connection))
{
sqlConnection.Open();
sqlCommand = new SqlCommand("select * from Reservation where
iRoomNo=@p1 and @p2 between dCheckInDate and dCheckoutDate", sqlConnection);
sqlCommand.Parameters.AddWithValue("@p1", roomNo);
sqlCommand.Parameters.AddWithValue("@p2", checkInDate);
sqlDataAdapter = new SqlDataAdapter(sqlCommand);
dataset = new DataSet();
sqlDataAdapter.Fill(dataset);
if (dataset.Tables[0].Rows.Count > 0)
{
return true;
}
return false;
}
}
public DataTable GetRoomDetails(string roomType)
{
try
{
using (sqlConnection = new SqlConnection(connection))
{
sqlConnection.Open();
sqlCommand = new SqlCommand("select * from Roomtype where
Roomtype=@p1", sqlConnection);
sqlCommand.Parameters.AddWithValue("@p1", roomType);
sqlDataAdapter = new SqlDataAdapter(sqlCommand);
dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return dataTable;
}
public static DataTable SearchRoomByDate(DateTime searchDate)
{
DataTable dataTable = null;
try
{
using (sqlConnection = new SqlConnection(connection))
{
sqlConnection.Open();
sqlCommand = new SqlCommand("select * from Reservation where
@p1 between dCheckInDate and dCheckoutDate", sqlConnection);
sqlCommand.Parameters.AddWithValue("@p1", searchDate);
sqlDataAdapter = new SqlDataAdapter(sqlCommand);
dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return dataTable;
}
}
} 

C Sharp Online Compiler

Write, Run & Share C# code online using OneCompiler's C# online compiler for free. It's one of the robust, feature-rich online compilers for C# language, running on the latest version 8.0. Getting started with the OneCompiler's C# compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C# and start coding.

Read inputs from stdin

OneCompiler's C# online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

using System;
 
namespace Sample
{
  class Test
    {
      public static void Main(string[] args)
       {
         string name;
         name = Console.ReadLine();
         Console.WriteLine("Hello {0} ", name);
	}
     }
}

About C Sharp

C# is a general purpose object-oriented programming language by Microsoft. Though initially it was developed as part of .net but later it was approved by ECMA and ISO standards.

You can use C# to create variety of applications, like web, windows, mobile, console applications and much more using Visual studio.

Syntax help

Data types

Data TypeDescriptionRangesize
intTo store integers-2,147,483,648 to 2,147,483,6474 bytes
doubleto store large floating point numbers with decimalscan store 15 decimal digits8 bytes
floatto store floating point numbers with decimalscan store upto 7 decimal digits4 bytes
charto store single characters-2 bytes
stringto stores text-2 bytes per character
boolto stores either true or false-1 bit

Variables

Syntax

datatype variable-name = value;

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition or set of few conditions IF-ELSE is used.

if(conditional-expression) {
   // code
} 
else {
   // code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement) {
  // code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {
 // code 
}

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition);

Arrays

Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

Syntax

data-type[] array-name;

Methods

Method is a set of statements which gets executed only when they are called. Call the method name in the main function to execute the method.

Syntax

static void method-name() 
{
  // code to be executed
}