[英]Passing JSON type as parameter to SQL Server 2016 stored procedure using ADO.Net in ASP.Net Core project
Can someone give example how to pass JSON type as parameter to SQL Server 2016 stored procedure using ADO.Net in C# ASP.Net Core Web Api project ?有人可以举例说明如何在 C# ASP.Net Core Web Api 项目中使用 ADO.Net 将 JSON 类型作为参数传递给 SQL Server 2016 存储过程吗? I want to see example of SQL Server 2016 stored procedure and pass of JSON type in C# ASP.Net Core Web Api.我想在 C# ASP.Net Core Web Api 中查看 SQL Server 2016 存储过程和 JSON 类型传递的示例。
There is no json
data type in SQL Server you can simply send your json
as nvarchar(max)
to a stored procedure. SQL Server 中没有json
数据类型,您可以简单地将json
作为nvarchar(max)
发送到存储过程。
If you want to map your json to table you can use use OPENJSON
to convert data to rows and columns.如果要将 json 映射到表,可以使用OPENJSON
将数据转换为行和列。
CREATE PROCEDURE SaveJSON
@pID int,
@pJson nvarchar(max)
AS
BEGIN
INSERT INTO [YourTable] ([ID], [JSONData])
VALUES (@pID, @pJson)
END
If you want to map json objects with table you can do this如果你想用表映射 json 对象,你可以这样做
//json would be something like this
[
{ "id" : 2,"name": "John"},
{ "id" : 5,"name": "John"}
]
INSERT INTO YourTable (id,Name)
SELECT id, name
FROM OPENJSON(@pJson)
WITH (id int,
name nvarchar(max))
Here is a very good and detailed article which will give you detailed idea to deal with json data
这是一篇非常好的和详细的文章,它将为您提供处理json data
详细想法
SQL Server 2016 do have native JSON support - a new JSON datatype (which is based on nvarchar
) is there, as well as a FOR JSON command to convert output from a query into JSON format SQL Server 2016 确实具有原生 JSON 支持 - 存在新的 JSON 数据类型(基于nvarchar
),以及用于将查询输出转换为 JSON 格式的 FOR JSON 命令
Microsoft did not include a separate JSON datatype - instead, there are a number of JSON functions (to package up database rows into JSON, or to parse JSON into relational data) which operate on columns of type NVARCHAR(n)
Microsoft 没有包含单独的 JSON 数据类型 - 相反,有许多 JSON 函数(将数据库行打包为 JSON,或将 JSON 解析为关系数据)对NVARCHAR(n)
类型的列进行操作
If you have JSON text, you can extract data from JSON or verify that JSON is properly formatted using built-in functions JSON_VALUE
, JSON_QUERY
, and ISJSON
.如果您有 JSON 文本,则可以从 JSON 中提取数据或使用内置函数JSON_VALUE
、 JSON_QUERY
和ISJSON
验证 JSON 的格式是否正确。 For more advanced querying and analysis, the OPENJSON
function can transform an array of JSON objects into a set of rows.对于更高级的查询和分析, OPENJSON
函数可以将一组 JSON 对象转换为一组行。 Any SQL query can be executed on the returned result set.可以对返回的结果集执行任何 SQL 查询。 Finally, there is the FOR JSON
clause that enables you to format query results as JSON text.最后,还有FOR JSON
子句,可让您将查询结果格式化为 JSON 文本。
So, I recommend you use NVARCHAR(MAX)
as your stored procedure parameter.因此,我建议您使用NVARCHAR(MAX)
作为存储过程参数。
For a simple example that shows the C# and the SQL, please see: https://chris.koester.io/index.php/2018/03/21/load-json-into-sql-server-using-a-stored-procedure-and-csharp/有关显示 C# 和 SQL 的简单示例,请参阅: https : //chris.koester.io/index.php/2018/03/21/load-json-into-sql-server-using-a-stored -procedure-and-csharp/
You probably have some higher level data layer approach in your C#, like with EF or something, but this will let you get a simple test setup using a good ol' ADO.Net SQLCommand.你可能在你的 C# 中有一些更高级别的数据层方法,比如 EF 或其他东西,但这会让你使用一个好的 ADO.Net SQLCommand 获得一个简单的测试设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.