[英]How to return list item as JSON in ASP.NET MVC using stored procedure result?
I want to return some data from a SQL Server table;我想从 SQL Server 表中返回一些数据; I used a stored procedure like this:
我使用了这样的存储过程:
ALTER PROCEDURE [dbo].[SP_TITILESEARCH]
(@FLAG INT,
@Author VARCHAR(300) = NULL,
@Category VARCHAR(100) = NULL,
@Title VARCHAR(MAX) = NUll)
AS
BEGIN
IF (@FLAG = 1)
BEGIN
SELECT *
FROM SmartLibClientDB.dbo.LibraryMediaEntry
WHERE Title LIKE '%' + @Title + '%'
END
END
On the server-side, the stored procedure is executed like this:在服务器端,存储过程是这样执行的:
var media = new LibraryMediaEntry();
var db = new SchooberrySchoolEntities();
SqlParameter param1 = new SqlParameter("@Title", "Charpy Impact Test");
SqlParameter param2 = new SqlParameter("@FLAG", 1);
media = db.Database.SqlQuery<LibraryMediaEntry>("exec SP_TITILESEARCH @Title, @FLAG", param1, param2)
.ToList()
.FirstOrDefault(); // Getting error when executed
return Json(media, JsonRequestBehavior.AllowGet);
But the code throws an error:但是代码抛出一个错误:
Conversion failed when converting the varchar value 'Charpy Impact Test' to data type int.
将 varchar 值“Charpy Impact Test”转换为数据类型 int 时转换失败。
What I want to do is return the table data as JSON
is this the correct method to do this?我想要做的是将表数据返回为
JSON
这是执行此操作的正确方法吗? Or why the error?或者为什么会出错? I can't understand what I am doing wrong because the column the error asking to convert to
int
is actually a varchar
column我不明白我做错了什么,因为要求转换为
int
的错误列实际上是一个varchar
列
Note: I am using a stored procedure because the data I am returning is from a different database on the same server注意:我使用的是存储过程,因为我返回的数据来自同一服务器上的不同数据库
Conversion failed when converting the varchar value 'Charpy Impact Test' to data type int.
将 varchar 值“Charpy Impact Test”转换为数据类型 int 时转换失败。
This means that the @Title
parameter is expecting an int
(integer).这意味着
@Title
参数需要一个int
(整数)。 Charpy Impact Test
is not an integer so your stored procedure call fails. Charpy Impact Test
不是整数,因此您的存储过程调用失败。 Picking an integer value is the first step.选择一个整数值是第一步。
want to do is return the table data as JSON
想要做的是将表数据作为 JSON 返回
Your code:您的代码:
media = db.Database.SqlQuery<LibraryMediaEntry>("exec SP_TITILESEARCH @Title,@FLAG"
,param1
,param2)
.ToList()
.FirstOrDefault();
is calling .FirstOrDefault()
which will result in a singe row in media.正在调用
.FirstOrDefault()
这将导致媒体中的.FirstOrDefault()
。 Did you intend to have a single record returned?您是否打算返回单个记录?
There are a couple of possible issues with your code.您的代码有几个可能的问题。
Your stored proc param @FLAG
is defined as varchar(25)
.您存储的 proc 参数
@FLAG
被定义为varchar(25)
。 Yet, when populating it, you are passing an integer ( SqlParameter param2 = new SqlParameter("@FLAG",1);
).然而,在填充它时,您正在传递一个整数(
SqlParameter param2 = new SqlParameter("@FLAG",1);
)。 Change it to:将其更改为:
var param2 = new SqlParameter("@FLAG", "1"); var param2 = new SqlParameter("@FLAG", "1");
On the matter of the actual error you are getting... Check your LibraryMediaEntry object.关于您遇到的实际错误...检查您的 LibraryMediaEntry 对象。 Is the property that's getting populated an int by any chance?
是否有机会填充 int 的属性? That's probably where the problem lies.
这大概就是问题所在。
Finally, your stored proc.最后,您的存储过程。
SELECT * FROM
is bad form in a stored procedure. SELECT * FROM
在存储过程中是错误的形式。 Specify the actual fields you want.指定所需的实际字段。 It also helps with debugging.
它还有助于调试。
I couldn't find what caused the error but passing all parameters including the null did the job for me我找不到导致错误的原因,但传递包括 null 在内的所有参数为我完成了这项工作
var DB = new dbEntities();
SqlParameter param1 = new SqlParameter("@FLAG", 1);
SqlParameter param2 = new SqlParameter("@Author", "");
SqlParameter Param3 = new SqlParameter("@Category", "");
SqlParameter param4 = new SqlParameter("@Title", Convert.ToString(id));
var medias = db.Database.SqlQuery<LibraryMediaEntry>("exec SP_TITILESEARCH @FLAG,@Author,@Category,@Title", param1, param2, Param3, param4).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.