在Android中从SQL DB检索数据的最佳方法

时间:2014-01-20 09:03:48

标签: android asp.net sql web-services

我正在使用SOAP方法使用Asp.NET Webservice从Android App中的SQL数据库中检索数据 我也看过JSON方法,但它使用的是php。

哪个是最好的方法???我有Windows Server和SQL数据库所以我将只使用Asp.NET Web服务。

1 个答案:

答案 0 :(得分:0)

如果你有SqlServer意味着你可以去 asp.Net Json Webservices 。 Json非常容易解析,我自己也只使用带有json格式的.net webservice。

解析对移动开发人员来说既快又容易

public class Employee
{
    public string Name { get; set; }
    public string Company { get; set; }
    public string Address { get; set; }
    public string Phone { get; set; }
    public string Country { get; set; }
}

然后在Webserice中添加以下方法。

Json Array-Object 我们大多会使用

     [WebMethod]
     [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
     public static string GenerateTopicListToJSon()
            {
                try

                {

                    //Do ur query in GetMethod() to retrieve from DB     
                    List<Employee> lstEmployee = GetMethod();
                    var topic = from t in lstEmployee
                                select new
                                {
                                    Name= t.TopicID,
                                    Company = t.Company,
                                    Address = t.Address,
                        Phone = t.Phone,
                                    Country = t.Country

                                };


                    var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    return oSerializer.Serialize(topic);

                }

希望它有助于你的伙伴......

干杯。