简体   繁体   English

在 ASP.Net Core 项目中使用 ADO.Net 将 JSON 类型作为参数传递给 SQL Server 2016 存储过程

[英]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_VALUEJSON_QUERYISJSON验证 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.

相关问题 如何在 ASP.NET Core MVC 中使用 ADO.NET 将参数添加到存储过程? - How can I add parameters to a stored procedure using ADO.NET in ASP.NET Core MVC? 将 ASP.NET Core razor 页面中的下拉列表绑定到 ado.net 存储过程 - Binding a dropdown in ASP.NET Core razor page to ado.net stored procedure 将对象列表传递到SQL Server存储过程并使用asp.net核心插入记录 - Passing list of objects to a SQL Server stored procedure and insert records using asp.net core 使用ADO.NET连接到SQL Server存储过程 - Connect to SQL Server Stored procedure with ADO.NET 如何在ado.net中使用输出参数并选择sql server存储过程的查询结果? - How to use output parameter and select query result of a sql server stored procedure in ado.net? 使用ADO.NET for HANA API将数据表传递到HANA接受表类型中的存储过程作为输入 - Passing Data Table to stored procedure in HANA accepting table type as input using ADO.NET for HANA API 使用ADO.Net在模型中脚手架调用SQL存储过程 - Scaffolding calling SQL stored procedure in model using ADO.Net 使用ExecuteReader时运行SQL Server存储过程的Ado.net无法检索输出值 - Ado.net running SQL Server stored procedure can't retrieve output value when using ExecuteReader 存储过程-ADO.NET - Stored procedure - ADO.NET 防止在 ASP.NET 中与 ADO.NET 并发执行存储过程 - Prevent concurrent execution of stored procedure with ADO.NET in ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM