1

你好,我建立了一个与 iPhone 通信的 web 服务。

我想在表格中显示来自所选街道的房间,为此我必须通过 asp.net 网络服务将一些 ID 从我的 iPhone 与 JSON 传递到 sql 数据库。

我的网络方法如下所示:

  [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);
    }

我的 sql 语句应该是这样的:

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

如何在我的 sql 语句中使用传递的参数来过滤房间?

4

1 回答 1

1
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())
            {

            }
        }
    }
}
于 2012-06-13T06:50:42.783 回答