简体   繁体   English

将C#中的复杂对象插入到没有实体框架和数据表的SQL Server中

[英]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; 重要的注意事项是;

  • Values are parameterized 值已参数化
  • The statements execute as a transaction (and so will all get rolled back in event of an issue 这些语句作为一个事务执行(因此,如果发生问题,所有语句都会回滚

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.

相关问题 C#:SQL Server 批量插入性能不佳(实体框架 5) - C# : SQL Server Bulk insert poor performance (Entity Framework 5) C#实体框架插入关系表? - C# Entity Framework insert relationship tables? 实体框架LINQ加快了将SQL数据转换为C#对象的速度 - Entity framework LINQ speed converting SQL data into C# objects 优化插入、删除、更新 SQL Server 表的简单方法包括具有图像数据类型的列,无需实体框架,分 3 步 (C#) - Simple way to optimize INSERT, DELETE, UPDATE SQL Server table includes columns with image data-type without Entity Framework in 3 steps (C#) 从具有外键的表中获取数据 C# 实体框架 - Getting data from tables with foreign keys C# entity framework 有没有办法在没有实体框架的情况下在C#中映射存储过程? - Is there a way to map stored procedure from SQL Server in C# without Entity Framework? 数据从 C# 插入 SQL Server - Data insert into SQL Server from C# C#实体框架/ SQL Server匹配数据的数量 - C# Entity Framework / SQL Server count number of matched data 如何使用 C# 将数据插入到多个 SQL 服务器表中? - How to insert data into multiple SQL Server tables using C#? 如何从 SQL 服务器紧凑型 C# 在 Entity Framework 中更快地读取数据 - How to read data faster in Entity Framework from SQL Server Compact C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM