0
string sqlStatement = "SELECT Orders.[ID], Orders.[Checkintime], Orders.[RoomPrice], Orders.[OrderNo], Particulars.FirstName, Particulars.LastName FROM Orders, where Checkintime between '" + dateOnly + "' and '" + endDateOnly + "', Particulars;";

I tried using this statement to select information from my database but this statement has syntax errors from the FROM clause

fubo
  • 44,811
  • 17
  • 103
  • 137
Jace
  • 37
  • 3

3 Answers3

2

I suppose that the two tables (Orders and Particulars) are joined by some field that acts as a foreignkey. So you should have a OrderID field in the Particulars table that links every 'particular' to the respective Order.

If this is the case then your query should be something like this

string sqlStatement = @"SELECT Orders.[ID], Orders.[Checkintime], 
                        Orders.[RoomPrice], Orders.[OrderNo], 
                        Particulars.FirstName, Particulars.LastName 
                        FROM Orders INNER JOIN Particulars 
                        ON Orders.[ID] = Particulars.[OrderID]
                        where Checkintime between '" + dateOnly + 
                        "' and '" + endDateOnly + "'";

However this approach with string concatenation is prone to other kind of errors like parsing problems and Sql injection, better use a parameterized query

string sqlStatement = @"SELECT Orders.[ID], Orders.[Checkintime], 
                        Orders.[RoomPrice], Orders.[OrderNo], 
                        Particulars.FirstName, Particulars.LastName 
                        FROM Orders INNER JOIN Particulars 
                        ON Orders.[ID] = Particulars.[OrderID]
                        where Checkintime between @init AND @end";
using(SqlConnection cnn = new SqlConnection(.....))
using(SqlCommand cmd = new SqlCommand(sqlStatement, cnn))
{
     cnn.Open();
     cmd.Parameters.Add("@init", SqlDbType.DateTime).Value = dateOnly;
     cmd.Parameters.Add("@end", SqlDbType.DateTime).Value = endDateOnly;

     .... remainder of your code that reads back your data.....
}

Please note, the value supplied to the Parameter.Value should be a DateTime variable not a string....

Steve
  • 213,761
  • 22
  • 232
  • 286
1
  1. Remove that , here Orders, where

  2. That , Particulars; doesn't makes sence as well probably you should use a join ... FROM Orders join Particulars ON Orders.ID = Particulars.ID or what the ID-column is called

  3. Depending on your Culture the DateTime could cause a problem when it's passed as a string value '04/08/2015 21:52:39'. e.g. in some cultures day and month are swapped. You can do dateOnly.ToString("yyyy-MM-dd") or better use parameters and pass a DateTime object to avoids injection attacks additional

fubo
  • 44,811
  • 17
  • 103
  • 137
  • I removed the comma and it showed me this "Syntax error (comma) in query expression 'Checkintime between '04/08/2015 21:52:39' and '05/08/2015 00:00:00', Particulars'."} – Jace Aug 04 '15 at 13:53
0

you need a join query:

string sqlStatement = "SELECT Orders.[ID], Orders.[Checkintime], Orders.[RoomPrice], Orders.[OrderNo], Particulars.FirstName, Particulars.LastName FROM Orders inner join Particulars  on Orders.CommonField=Particulars.CommonField
where Checkintime between '" + dateOnly + "' and '" + endDateOnly + "'";
apomene
  • 14,282
  • 9
  • 46
  • 72