我使用asmx用sqlconnection完成了webservice。现在我需要使用带有json响应的asp .net web api开发webservice。 [英] I have done webservice with sqlconnection using asmx . now i need to develop webservice using asp .net web api with json response.

查看:96
本文介绍了我使用asmx用sqlconnection完成了webservice。现在我需要使用带有json响应的asp .net web api开发webservice。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asmx用sqlconnection完成了webservice。



第1步:asmx.cs



I have done webservice with sqlconnection using asmx .

step 1: asmx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace webserxml
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public partial class WebService1 : System.Web.Services.WebService
{

[WebMethod]
public XmlElement GetUserDetails(string sqlcmd)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconn"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand(sqlcmd, con);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
XmlDataDocument xmldata = new XmlDataDocument(ds);
XmlElement xmlElement = xmldata.DocumentElement;
return xmlElement;
}
}
}





第2步:网络配置文件





step 2: web config file

<connectionstrings>
<add name="sqlconn" connectionstring="data source=sourcename;initial catalog=databasename;user id=idname;password=pwd">
providerName="System.Data.SqlClient" />
</add></connectionstrings>





现在我运行该应用程序。我将在webservice中搜索colum,如下所示:



参数值



sqlcmd select *来自sportstablename



调用btn。

所以,如果我点击调用,我将获得xpos格式的spoststable数据。对?。在sqlcmd中,我可以编写任何类型的查询,并可以从数据库中获取数据。所以我需要在asp.net web api中开发,返回json格式。因为我是asp.net web api的新手。浏览时我遇到了wcf服务和asp.net web api。更多的评论说去asp.net web api这是好的,并有更多的优势,以及asmx和wcf服务。所以,请使用asp.net web api以json格式提出上述相同的要求。







这里使用上面的连接字符串。我可以检索数据库中的任何现有表。很少有文章使用sql来LINQ,它只需要一个表关系和几个文章ADO .NET实体也可以连接任何特定的表,它们都不是灵活的而不是用户定义的。以上asmx webserce我提到的更多用户为我的项目定义。每个人都只使用连接字符串连接数据库并可以检索但是,我想要使用asp.net web api进行JSON响应。请用简短的例子给出完整的解释。



非常感谢。



now i run the the application. i will have search colum in webservice right like follows:

parameter value

sqlcmd select * from sportstablename

invoke btn.
so, if i click invoke i will get data of spoststable in xml format. right?. in sqlcmd, i can write any kind of query and can get data from data base. so the same thing i need to develop in asp.net web api which return json format. becuase i new for asp.net web api. while browsing i came across wcf service and asp.net web api. more comments said go for asp.net web api which is good and having more advantantage compring asmx and wcf service. so, please the above same requirment i need by in json format using asp.net web api.



here using above connectionstring. i can retrieve whatever existing table in database. few article did using sql to LINQ which takes only one table relation and few article ADO .NET entity that also does connection any particular table which both are not flexile and not user defined. the above asmx webserce what i mentioned is more user defined for my project. everyone just connect database using connection string and can retrieve. but the thing entirly i wanted to do JSON response using asp.net web api. pls give full explanation with brief example.

thanks a lot.

推荐答案

http:// www .asp.net / web-api

此链接包含对示例的完整说明
http://www.asp.net/web-api
this link contains full explaination with examples


这篇关于我使用asmx用sqlconnection完成了webservice。现在我需要使用带有json响应的asp .net web api开发webservice。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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