简体   繁体   English

SQL 2008:将数据行作为JSON返回?

[英]SQL 2008: returning data rows as JSON?

I think this question is like clay pidgeon shooting.. "pull... bang!" 我觉得这个问题就像粘土皮江射击......“拉......砰!” .. shot down.. but nevertheless, it's worth asking I believe. ..击落......但是,我相信这是值得的。

Lots of JS frameworks etc use JSON these days, and for good reason I know. 现在有很多JS框架等使用JSON,我知道这个理由很充分。 The classic question is "where to transform the data to JSON". 经典问题是“将数据转换为JSON的位置”。

I understand that at some point in the pipeline, you have to convert the data to JSON, be it in the data access layer (I am looking at JSON.NET) or I believe in .NET 4.x there are methods to output/serialize as JSON. 我理解在管道中的某个时刻,你必须将数据转换为JSON,无论是在数据访问层(我在看JSON.NET)还是我相信.NET 4.x都有输出方法/序列化为JSON。

So the question is: Is it really a bad idea to contemplate a SQL function to output as JSON? 所以问题是:考虑将SQL函数输出为JSON真的是个坏主意吗?

Qualifier: I understand trying to output 1000's of rows like that isn't a good idea - in fact not really a good idea for web apps either way unless you really have to. 限定符:我理解尝试输出这样的1000行并不是一个好主意 - 实际上对于Web应用程序来说这不是一个好主意,除非你真的需要。 For my requirement, I need possibly 100 rows at a time... 根据我的要求,我一次可能需要100行......

The answer really is: it depends . 答案真的是: 它取决于

If your application is a small one that doesn't receive much use, then by all means do it in the database. 如果您的应用程序是一个没有太多用处的小应用程序,那么无论如何都要在数据库中执行它。 The thing to bear in mind though is, what happens when your application is being used by 10x as many users in 12 months time ? 需要注意的是, 当您的应用程序在12个月内被10倍用户使用时会发生什么

If it makes it quick, simple and easy to implement JSON encoding in your stored procedures, rather than in your web code and allows you to get your app out and in use, then that's clearly the way to go. 如果它能够快速,简单,轻松地在您的存储过程中实现JSON编码,而不是在您的Web代码中,并允许您将应用程序移出并使用,那么这显然是可行的方法。 That said, it really doesn't take that much work to do it "properly" with solutions that have been suggested in other answers. 也就是说,使用其他答案中提出的解决方案“正确”完成它并不需要那么多工作。

The long and short of it is, take the solution that best fits your current needs, whilst thinking about the impact it'll have if you need to change it in the future. 长期和短期是,采取最符合您当前需求的解决方案,同时考虑如果您将来需要更改它将产生的影响。

这就是[WebMethod](WebMethodAttribute)存在的原因。

Best to load the data to to the piece of program and then return it as JSON. 最好将数据加载到程序块,然后将其作为JSON返回。

.NET 4 has a support for returning json, and i did it as a part of one ASP.NET MVC site and it was fairly simple and straightforward. .NET 4支持返回json,我将它作为一个ASP.NET MVC站点的一部分来做,它非常简单明了。

I recommend to move the transformation out of the sql server 我建议将转换移出sql server

I agree with the other respondents that this is better done in your application code. 我同意其他受访者的意见,即在您的应用程序代码中做得更好。 However... this is theoretically possible using SQL Server's ability to include CLR assemblies in the database using create assembly syntax. 但是......理论上可以使用SQL Server使用create assembly语法在数据库中包含CLR程序 The choice is really yours. 选择真的是你的。 You could create an assembly to do the translation in .net, define that assembly to SQL Server and then use contained method(s) to serialize to JSON as return values from your stored procedures... 您可以创建一个程序集来在.net中进行转换,将该程序集定义到SQL Server,然后使用包含的方法序列化为JSON作为存储过程的返回值...

Better to load it using your standard data access technique and then convert to JSON. 最好使用标准数据访问技术加载它,然后转换为JSON。 You can then use it in standard objects in .NET as well as your client side javascript. 然后,您可以在.NET中的标准对象以及客户端javascript中使用它。

If using .net mvc you serialize your results in your controllers and output a JsonResult, there's a method Controller.Json() that does this for you. 如果使用.net mvc在控制器中序列化结果并输出JsonResult,则有一个方法Controller.Json()为您执行此操作。 If using webforms an http handler and the JavascriptSerializer class would be the way to go. 如果使用webforms,则可以使用http处理程序和JavascriptSerializer类。

Hey thanks for all the responses.. it still amazes me how many people out there have the time to help. 嘿,感谢所有的回复......我仍然惊讶于有多少人有时间提供帮助。

All very good points, and certainly confirmed my feeling of letting the app/layer do the conversion work - as the glue between the actual data and frontend. 所有非常好的观点,肯定证实了我让app / layer进行转换工作的感觉 - 作为实际数据和前端之间的粘合剂。 I guess I haven't kept up too much with MVC or SQL-2008, and so was unsure if there were some nuggets worth tracking down. 我想我没有跟MVC或SQL-2008保持太多,所以不确定是否有一些值得追踪的掘金。

As it worked out (following some links posted here, and further fishing) I have opted to do the following for the time being (stuck back using .NET 3.5 and no MVC right now..): 随着它的发布(在这里发布的一些链接,以及进一步的钓鱼)我暂时选择了以下操作(使用.NET 3.5并且现在没有MVC卡住了...):

  1. Getting the SQL data as a datatable/datareader 将SQL数据作为数据表/ datareader获取
  2. Using a simple datatable > collection (dictionary) conversion for a serializable list 对可序列化列表使用简单的数据表>集合(字典)转换
  3. Because right now I am using an ASHX page to act as the broker to the javascript (ie via a JQuery AJAX call), within my ASHX page I have: 因为我现在使用ASHX页面充当javascript的代理(即通过JQuery AJAX调用),在我的ASHX页面中我有:

    context.Response.ContentType = "application/json"; context.Response.ContentType =“application / json”; System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer(); System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();

  4. I can then issue: json.serialize(<>) 然后我可以发出:json.serialize(<>)

Might seem a bit backward, but it works fine.. and the main caveat is that it is not ever returning huge amounts of data at a time. 可能看起来有点落后,但它工作得很好..主要的警告是,它不会一次返回大量数据。

Once again, thanks for all the repsonses! 再一次,感谢所有的回复!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM