I have a SQLDataAdapter and I want to retrieve data from my database in SQL Server 2005 . Supposing I have two tables named Guest, Sales and one of the columns is GuestFirstName. I want to retrieve One record(GuestFirstName) from table Guest that has the same GuestID as in Sales GuestID. Also in the Sales table the column SalesStatus needs to contain the text 'ToGuestInvoice'.
Directly in SQL Server 2005 I can perform this query with the following syntax:
Expand|Select|Wrap|Line Numbers
- SELECT g.GuestFirstName as GuestFirstName
- From Guest AS g, Sales As s
- WHERE g.GuestID = s.GuestID AND s.SalesStatus = 'ToGuestInvoice'
- GROUP BY g.GuestFirstName
How can that be done?
I wrote this code: (Visual Studio 2005-C#)
Expand|Select|Wrap|Line Numbers
- string sqlq = @" Select g.GuestFirstName from Guest g, Sales s where g.GuestID = s.GuestID And s.SalesStatus Like 'ToGuestInvoice%' GROUP BY g.GuestFirstName ";
- SqlDataAdapter da = new SqlDataAdapter(sqlq, dbConnection);
- myCommand.CommandText = sqlq;
- myCommand.Parameters.Add("@GuestFirstName",SqlDbType.VarChar).Value = da;