【发布时间】:2014-01-04 21:41:07
【问题描述】:
我创建了一个小项目,可以通过 RESTful WCF 服务从 iPhone 读取数据并将数据插入到 sql server。
我已通过以下方法成功读取数据:
1- 我创建了一个 wcf Web 服务,它使用表 Employees(firstname,lastname,salary) 从 Sql server 读取数据:
"41.142.251.142/JsonWcfService/GetEmployees.svc/json/employees"
2- 我在 xcode 5.0.2 中创建了一个新项目,并添加了一个文本字段 (viewData.text) 来显示 Web 服务检索到的数据。 3- 我在 viewController.m 中添加了以下指令:
"#define WcfSeviceURL [NSURL URLWithString: @"41.142.251.142/JsonWcfService/GetEmployees.svc/json/employees"]"
3- 在 (void)viewDidLoad 方法中,我实现了以下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:WcfSeviceURL options:NSDataReadingUncached error:&error];
if(!error)
{
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&error];
NSMutableArray *array= [json objectForKey:@"GetAllEmployeesMethodResult"];
for(int i=0; i< array.count; i++)
{
NSDictionary *empInfo= [array objectAtIndex:i];
NSString *first = [empInfo objectForKey:@"firstname"];
NSString *last = [empInfo objectForKey:@"lastname"];
NSString *salary = [empInfo objectForKey:@"salary"];
//Take out whitespaces from String
NSString *firstname = [first
stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *lastname = [last
stringByReplacingOccurrencesOfString:@" " withString:@""];
viewData.text= [viewData.text stringByAppendingString:[NSString stringWithFormat:@"%@ %@ makes $%@.00 per year.\n",firstname,lastname,salary]];
}
}
}
检查以下链接:http://www.codeproject.com/Articles/405189/How-to-access-SQL-database-from-an-iPhone-app-Via。
正如我所提到的,我可以毫无问题地从我的 iPhone 读取数据。
所以第二步是如何将数据从 iPhone 写入和插入到 sql server 中。 为此,我首先创建了在我的网络服务中插入数据的方法:
在 WCF 界面中:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/InsertEmployee/{id1}/{id2}/{id3}")]
bool InsertEmployeeMethod(string id1,string id2, string id3);
实施中:
public bool InsertEmployeeMethod(string id1,string id2, string id3)
{
int success = 0;
using (SqlConnection conn = new SqlConnection("server=(local);database=EmpDB;Integrated Security=SSPI;"))
{
conn.Open();
decimal value= Decimal.Parse(id3);
string cmdStr = string.Format("INSERT INTO EmpInfo VALUES('{0}','{1}',{2})",id1,id2,value);
SqlCommand cmd = new SqlCommand(cmdStr, conn);
success = cmd.ExecuteNonQuery();
conn.Close();
}
return (success != 0 ? true : false);
}
所以要测试这个 web servcie 方法,请使用:
"41.142.251.142/JsonWcfService/GetEmployees.svc/json/InsertEmployee/myName/MylastName/6565"
然后,为了从 iPhone 使用此方法,我使用了以下方法: 我声明了定义指令:
"#define BaseWcfUrl [NSURL URLWithString:
@"41.142.251.142/JsonWcfService/GetEmployees.svc/json/InsertEmployee/{id1}/{id2}/{id3}"]"
然后我实现了与点击按钮相关的Insert Employee方法。
-(void) insertEmployeeMethod
{
if(firstname.text.length && lastname.text.length && salary.text.length)
{
NSString *str = [BaseWcfUrl stringByAppendingFormat:@"InsertEmployee/%@/%@/%@",firstname.text,lastname.text,salary.text];
NSURL *WcfServiceURL = [NSURL URLWithString:str];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:WcfServiceURL];
[request setHTTPMethod:@"POST"];
// connect to the web
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// NSString *respStr = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:respData
options:NSJSONReadingMutableContainers
error:&error];
NSNumber *isSuccessNumber = (NSNumber*)[json objectForKey:@"InsertEmployeeMethodResult"];
//创建一些标签字段来显示状态
status.text = (isSuccessNumber && [isSuccessNumber boolValue] == YES) ? [NSString stringWithFormat:@"Inserted %@, %@",firstname.text,lastname.text]:[NSString stringWithFormat:@"Failed to insert %@, %@",firstname.text,lastname.text];
}
}
但这里的问题在于以下说明:
NSString *str = [BaseWcfUrl stringByAppendingFormat:@"InsertEmployee/%@/%@/%@",firstname.text,lastname.text,salary.text];
系统总是用这一行返回消息'Data parameter nil',知道firstname.text、lastname.text、salary都填满了,我可以用@987654332看到它们的值@
你能帮忙吗? 提前致谢。
【问题讨论】:
标签: ios iphone sql wcf web-services