Eray Geveci
Eray Geveci

Reputation: 1129

how to use the passed parameters to the asp.net webservice in a sql statement?

hello i build a webservice to communicate with the iPhone.

i want to display the rooms from the selected street in a table, to do that i must to pass some id's from my iPhone with JSON to the sql database over a asp.net webservice.

my webmethod look like this:

  [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    [WebMethod]
    public string Room(string street_id, string country_id, string city_id)
    {
        return RoomHelper.Room(street_id, country_id, city_id);
    }

My sql statement should look like this:

SELECT Roomname FROM Room WHERE street_id = (passed street_id) AND country_id = (passed country_id) AND city_id = (passed city_id)

how can i use the passed parameters in my sql statement to filter the rooms ?

Upvotes: 1

Views: 799

Answers (1)

Alex R.
Alex R.

Reputation: 4754

using (SqlConnection conn = new SqlConnection("<my connection string>"))
{
    conn.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Roomname FROM Room WHERE street_id = @streetId AND country_id = @countryId AND city_id = @cityId", conn))
    {
        cmd.Parameters.AddWithValue("@streetId", street_id);
        cmd.Parameters.AddWithValue("@countryId", country_id);
        cmd.Parameters.AddWithValue("@cityId", city_id);
        using (var reader = cmd.ExecuteReader())
        {
            // retrieve your data per row or as you wish
            while (reader.Read())
            {

            }
        }
    }
}

Upvotes: 1

Related Questions