简体   繁体   English

ADO.net 更新没有 SQL?

[英]ADO.net update without SQL?

This might be a dumb question.这可能是个愚蠢的问题。 But does ADO.net support updating to a database without having to write SQL commands?但是 ADO.net 是否支持更新到数据库而无需编写 SQL 命令?

Example: I have a method that reads from database and keeps the rows in memory. I then modify some of the rows.示例:我有一个从数据库读取并将行保留在 memory 中的方法。然后我修改了一些行。 Is it then possible to make ADO.net update the newest changes to the database without having to write an update SQL statements but instead let ADO.net figure it out?那么是否可以让 ADO.net 更新数据库的最新更改,而不必编写更新 SQL 语句,而是让 ADO.net 弄清楚?

I am asking this because I might want to update at a much later point.我问这个是因为我可能想在晚些时候更新。 I could just store the SQL statements in a list but then I would be doing many updates instead of just one big one which would take longer time.我可以只将 SQL 语句存储在一个列表中,但这样我会做很多更新,而不是只做一个大的更新,这会花费更长的时间。

What you need is some sort of ORM , and ADO is not an ORM. So, no.您需要的是某种ORM ,而 ADO 不是 ORM。所以,不是。 You must write the SQL. You could maybe simplify things by writing a stored procedure, though.您必须编写 SQL。不过,您可以通过编写存储过程来简化事情。 Then you can use ADO parameters然后就可以使用ADO参数了

If you want, you can save your changes as objects in memory until you need to actually persist them.如果需要,您可以将更改保存为 memory 中的对象,直到您真正需要持久化它们为止。 Then you can have a mapper that will take the object and write the SQL for you.然后你可以有一个映射器,它将获取 object 并为你编写 SQL。 However, then you are redoing some of the work of what is already done in an ORM但是,您正在重做 ORM 中已经完成的一些工作

Like the sql you used to get the data, you need sql to put the data.像你以前拿数据的sql,放数据就需要sql。 It also needs to update what column to update.它还需要更新要更新的列。 I don't think it can be automatic.我不认为它可以是自动的。 Or use the Entity Framework.或者使用实体框架。 Probably saving the objects to be updates (IDs) is the way to go or update instantly.可能保存要更新的对象(ID)是到 go 或立即更新的方式。

ADO.NET supports DataAdapters and DataSets which allow you to do the following: ADO.NET 支持允许您执行以下操作的 DataAdapter 和 DataSet:

  1. Manipulate data within your DataSet.在您的 DataSet 中操作数据。
  2. Push changes to the database by passing your DataSet as a parameter to the Update method of the DataAdapter.通过将您的 DataSet 作为参数传递给 DataAdapter 的Update方法,将更改推送到数据库。

In order to get the DataAdapter to push the changes it is necessary to specify insert, update, and delete commands.为了让 DataAdapter 推送更改,必须指定插入、更新和删除命令。 You will have to specify some sql in your command configuration but it is like a template of the sql statement that will update each row that you operate upon rather than your having to manually track changes.您必须在命令配置中指定一些sql,但它就像 sql 语句的模板,它将更新您操作的每一行,而不是您必须手动跟踪更改。

Once you have configured your commands, use the UPDATE method with the DataSet as the parameter and it will persist your changes based on your commands.配置命令后,使用 UPDATE 方法并将 DataSet 作为参数,它将根据您的命令保留更改。 You will not need to track the individual sql changes.您将不需要跟踪单个 sql 更改。

  1. A sample of configuring commands can be found here .可以在此处找到配置命令的示例。
  2. A sample of calling the update can be found here .可以在此处找到调用更新的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM