[英]Insert complex objects from C# into SQL Server without Entity Framework and data tables
How to insert complex objects from C# into SQL Server without using Entity Framework and data tables (I thought about Dapper.Contrib but it is only for entities). 如何在不使用Entity Framework和数据表的情况下将C#中的复杂对象插入SQL Server(我考虑过Dapper.Contrib,但它只适用于实体)。
I'm using stored procedures and Dapper for now, but I can insert only one object and also - only with dynamic parameters so it not what I looking for. 我现在正在使用存储过程和Dapper,但是我只能插入一个对象 - 只能使用动态参数,所以它不是我想要的。
For example: 例如:
DynamicParameters p = new DynamicParameters();
p.Add("ID", user.UserInformation.ID);
p.Add("DELEGATORID", user.UserRole.DelegateID ?? string.Empty);
p.Add("APPROVER", user.UserRole.Approver);
p.Add("DELEGATOR", user.UserRole.IsDelegator);
p.Add("SEARCHTEXT", searchText);
and then insert. 然后插入。 But I need to do 2 more inserts.
但我需要再做两次插入。
For example - I want to get 3 complex objects to insert in the stored procedure and then execute 3 stored procedure for each object 例如 - 我希望在存储过程中插入3个复杂对象,然后为每个对象执行3个存储过程
Thanks. 谢谢。
Ideally, you'd use an ORM that takes care of this but one option is to use raw SQL strings in that you build the entire query as a string (or in several strings), parameterize it and execute it. 理想情况下,您可以使用ORM来处理这个问题,但是一个选项是使用原始SQL字符串,因为您将整个查询构建为字符串(或多个字符串),对其进行参数化并执行它。 A quick google gives and example here;
一个快速的谷歌给出了这里的例子; https://codereview.stackexchange.com/questions/59863/transaction-handling-for-multiple-sql-statements .
https://codereview.stackexchange.com/questions/59863/transaction-handling-for-multiple-sql-statements 。 Important points of note are;
重要的注意事项是;
How to insert complex objects from C# into SQL Server without using Entity Framework and data tables
如何在不使用Entity Framework和数据表的情况下将C#中的复杂对象插入SQL Server
SQL Server can parse XML , and newer versions can also parse JSON . SQL Server可以解析XML ,而较新的版本也可以解析JSON 。 So the simplest way to insert shaped data into SQL Server without an ORM is to send an XML or JSON document and parse it on the server.
因此,在没有ORM的情况下将形状数据插入SQL Server的最简单方法是发送XML或JSON文档并在服务器上解析它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.