我建立一个应用程序,我选择一个城市和一个街道的国家。在选择之后,它将id发送到webservice,我得到所有可用于所选地点的房间。但是,如果我想选择多个城市或多个街道呢?我正在寻找一个没有运气的动态解决方案。
这是iOS中我的roomdownloader:
-(void)raumDownloader
{
NSMutableArray *object=[[NSMutableArray alloc]init];
NSLog(@"RaumklasseID: %@ StadtID: %@ GebäudeID: %@ RegionID: %@",raumklasseid, stadtid, gebaudeid, regionid);
NSString *emptyString = @"";
[object addObject:raumklasseid];
if (stadtid==nil)
{
[object addObject:emptyString];
}
else {
[object addObject:stadtid];
}
if (gebaudeid==nil)
{
[object addObject:emptyString];
}else {
[object addObject:gebaudeid];
}
if (regionid==nil)
{
[object addObject:emptyString];
}else {
[object addObject:regionid];
}
NSMutableArray *key =[[NSMutableArray alloc]init];
[key addObject:@"RAUMKLASSE_ID"];
[key addObject:@"STADT_ID"];
[key addObject:@"GEBAEUDE_ID"];
[key addObject:@"REGION_ID"];
NSDictionary* info = [[NSDictionary alloc]initWithObjects:object forKeys:key];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:info
options:NSJSONWritingPrettyPrinted error:nil];
NSString *name = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://nexxtsolutions-entwicklung.de/Webdienst22/service1.asmx/Raum"]];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSData *reqData = [NSData dataWithBytes:[name UTF8String] length:[name length]];
[request setHTTPBody:reqData];
NSURLResponse *response =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init ];
dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
raumeArray = [dict objectForKey:@"d"];
for(int n=0; n<[raumeArray count]; n++)
{
NSLog(@"AUSGABE: %@",[[raumeArray objectAtIndex:n] objectForKey:@"RaumName"]);
}
}
你可以看到我有4个静态参数,我发送到网络服务:
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public List<RaumHelper.RAUM> Raum(string RAUMKLASSE_ID, string STADT_ID, string GEBAEUDE_ID, string REGION_ID)
{
return RaumHelper.Raum(RAUMKLASSE_ID, STADT_ID, GEBAEUDE_ID, REGION_ID);
}
并且助手类使用sql查询中的静态参数并将房间发送回iPhone:
internal static List<RAUM> Raum( string RAUMKLASSE_ID, string STADT_ID, string GEBAEUDE_ID, string REGION_ID)
{
List<RAUM> strasseObject = new List<RAUM>();
using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT-V2;Integrated Security=true;"))
using (SqlCommand cmd = new SqlCommand(@"SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID, ra.BEZEICHNUNG AS raumBEZEICHNUNG FROM RAUM r, RAUMATTRIBUTE ra WHERE RAUMKLASSE_ID = ISNULL(@Raumklasse_ID, RAUMKLASSE_ID) AND STADT_ID = ISNULL(@Stadt_ID, STADT_ID) AND GEBAEUDE_ID = ISNULL(@Gebaeude_ID, GEBAEUDE_ID) AND REGION_ID = ISNULL(@Region_ID, REGION_ID) INNER JOIN RAZUORDNUNG ON RAZUORDNUNG.RAUM_ID = RAUM.ID
INNER JOIN RAUMATTRIBUTE ON RAZUORDNUNG.RAUMATTRIBUTE_ID = RAUMATTRIBUTE.", con))
{
con.Open();
if (!StringExtensions.IsNullOrWhiteSpace(RAUMKLASSE_ID))
cmd.Parameters.AddWithValue("@Raumklasse_ID", RAUMKLASSE_ID);
else
cmd.Parameters.AddWithValue("@Raumklasse_ID", DBNull.Value);
if (!StringExtensions.IsNullOrWhiteSpace(STADT_ID))
cmd.Parameters.AddWithValue("@Stadt_ID", STADT_ID);
else
cmd.Parameters.AddWithValue("@Stadt_ID", DBNull.Value);
if (!StringExtensions.IsNullOrWhiteSpace(GEBAEUDE_ID))
cmd.Parameters.AddWithValue("@Gebaeude_ID", GEBAEUDE_ID);
else
cmd.Parameters.AddWithValue("@Gebaeude_ID", DBNull.Value);
if (!StringExtensions.IsNullOrWhiteSpace(REGION_ID))
cmd.Parameters.AddWithValue("@Region_ID", REGION_ID);
else
cmd.Parameters.AddWithValue("@Region_ID", DBNull.Value);
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
if (rdr["BEZEICHNUNG"] != DBNull.Value && rdr["ID"] != DBNull.Value)
{
strasseObject.Add(new RAUM()
{
RaumName = rdr["BEZEICHNUNG"].ToString(),
RaumID = rdr["ID"].ToString()
});
}
}
}
}
return strasseObject;
}
如果我选择多个城市或街道,我的问题又是什么? id应该是动态发送的,也是sql查询。 在此先感谢您的帮助:)
答案 0 :(得分:2)
我不打算阅读所有代码,因为解决方案很简单。您不必为每个查询参数严格使用单个字符串,而是接受以逗号分隔的列表。当请求进入时,您将这些请求分解为数组,并在数据库中查询所有参数组合。
使客户端代码也具有灵活性,这样它就可以接受一个事物列表,而不仅仅是一个。
或许限制数据库查询的总量,这样有人就不会向您发送三个列表,每个列表包含五个项目,这些项目需要125个查询,并且会有太多数据一次性返回到手机。