我如何使用ASP.NET中的Web服务从数据库获取数据(返回JSON和XML) [英] How I get data from database using web service in ASP.NET (return JSON and XML)

查看:92
本文介绍了我如何使用ASP.NET中的Web服务从数据库获取数据(返回JSON和XML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我从数据库中获取数据(sqlserver)



我怎样才能得到请帮助我Frineds ...



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Services;
使用 System.Web.Script.Services;
使用 System.Web.Script.Serialization;
[WebService]

/// < 摘要 >
/// myserviceclass的摘要说明
/// < / summary >
public class myserviceclass
{
[WebMethod]
public employee2 [] GetEmployessXML()
{
employee2 [] emp = new employee2 [] {
new employee2()
{
Id = 101
Name = Nitin
余额= 10000
},
new employee2()
{
Id = 102
Name = Dinesh
余额= 100000
}
};
return \\ temp;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetEmplyeeJson()
{
employee2 [] emp = new employee2 [] {
new employee2()
{

Id = 101
Name = Pandu
balance = 1000
},
new employee2()
{
Id = 102
Name = Tanu
余额= 2000
}
};
return new JavaScriptSerializer()。Serialize(emp);
}
}





我的尝试:



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.Services;

使用System.Web.Script.Services;

使用System.Web。 Script.Serialization;

[WebService]



///< summary>

///摘要myserviceclass的描述

///

公共类myserviceclass

{

[WebMethod]

public employee2 [] GetEmployessXML()

{

employee2 [] emp = new employee2 [] {

new employee2()

{

Id = 101,

名称=Nitin,

余额= 10000

},

新员工2()

{

Id = 102,

名称=Dinesh,

余额= 100000

}

};

返回emp;

}

[WebMethod]

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

公共字符串GetEmplyeeJson()

{

employee2 [] emp = new employee2 [] {

new employee2()< br $>
{



Id = 101,

名称=Pandu,

余额= 1000

},

新员工2()

{

Id = 102,

Name =Tanu,

余额= 2000

}

};

返回新的JavaScriptSerializer()。Serialize(emp);

}

}

解决方案

Regardi第二种JSON方法:



请注意,如果你需要这一行:

 [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 





WebMethod必须是静态的,请看链接:

WebForm背后的代码中的静态WebMethod [ ^ ]和asp.net - 为什么WebMethod声明为静态? - Stack Overflow [ ^ ]



编辑:认为这是指需要静态方法的网络表单,但是你的web服务方法的实现看起来是正确的。



 [WebMethod] 
public static string GetEmplyeeJson(){...}





可以访问json数组,例如:

  var  json = JSON.parse(响应.d); 



注意使用.NET和AJAX时的.d asp.net - JSON中的.d是什么意思? - 堆栈溢出 [ ^ ]


here i am Get data from database(sqlserver)

how can i get please Help me Frineds...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
[WebService]

/// <summary>
/// Summary description for myserviceclass
/// </summary>
public class myserviceclass
{
    [WebMethod]
    public employee2[] GetEmployessXML()
    {
        employee2[] emp = new employee2[] {  
            new employee2()  
            {  
                Id=101,  
                Name="Nitin",  
                balance=10000  
            },  
            new employee2()  
            {  
                Id=102,  
                Name="Dinesh",  
                balance=100000  
            }  
        };
        return emp;
    }
    [WebMethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
	public string GetEmplyeeJson()
	{
        employee2[] emp = new employee2[]{
            new employee2()
            {

                Id=101,
                Name="Pandu",
                balance=1000
            },
            new employee2()
            {
                Id=102,
                Name="Tanu",
                balance=2000
            }
        };
        return new JavaScriptSerializer().Serialize(emp);
	}
}



What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
[WebService]

/// <summary>
/// Summary description for myserviceclass
///
public class myserviceclass
{
[WebMethod]
public employee2[] GetEmployessXML()
{
employee2[] emp = new employee2[] {
new employee2()
{
Id=101,
Name="Nitin",
balance=10000
},
new employee2()
{
Id=102,
Name="Dinesh",
balance=100000
}
};
return emp;
}
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string GetEmplyeeJson()
{
employee2[] emp = new employee2[]{
new employee2()
{

Id=101,
Name="Pandu",
balance=1000
},
new employee2()
{
Id=102,
Name="Tanu",
balance=2000
}
};
return new JavaScriptSerializer().Serialize(emp);
}
}

解决方案

Regarding the 2nd method for JSON:

Note sure if you require this line:

[ScriptMethod(ResponseFormat=ResponseFormat.Json)]



WebMethod must be static see links:
Static WebMethod in Code Behind WebForm[^] and asp.net - Why WebMethod declared as Static? - Stack Overflow[^]

Edit: thought this was referring to web forms where static methods would be required, but your implementation of webservice methods seeems correct.

[WebMethod]    
public static string GetEmplyeeJson() { ... } 



The json array can be accessed such as:

var json = JSON.parse(response.d);


Note the .d when using .NET and AJAX asp.net - What does .d in JSON mean? - Stack Overflow[^]


这篇关于我如何使用ASP.NET中的Web服务从数据库获取数据(返回JSON和XML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆