简体   繁体   English

为Linq DataContext的ExecuteCommand构造复杂的SQL

[英]Construct complex sql for Linq DataContext's ExecuteCommand

I have two tables in a database on an SQL Server 2008 R2 server. 我在SQL Server 2008 R2服务器上的数据库中有两个表。

Table1: 表格1:

Id, Value, Date

Table2: 表2:

Id, Value, Date

I am working on a ASP.Net MVC 4 project that utilizes Linq to SQL for database operations. 我正在使用ASP.Net MVC 4项目,该项目利用Linq to SQL进行数据库操作。 I need to build a SQL command for db.ExecuteCommand(). 我需要为db.ExecuteCommand()构建一个SQL命令。 No database Procedure or Function needed. 无需数据库过程或函数。

The SQL command should get {Value} and {Date} from Table1 depending on the "Id" entered by the user from the View. SQL命令应从表1中获取{Value}和{Date},具体取决于用户从视图中输入的“ Id”。 Then update {Value} and {Date} from all matching rows in Table2 where Tabl2.{Id} = {Id} of user input. 然后从表2中所有匹配的行更新{Value}和{Date},其中Tabl2。{Id} = {Id}用户输入。

Is ExecuteCommand able to handle complex SQL command like this? ExecuteCommand能够处理这样的复杂SQL命令吗? How to build the command then? 那么如何构建命令? Thanks! 谢谢!

你的意思是这样吗?

db.ExecuteCommand("UPDATE table2 SET value = (SELECT value from table1 WHERE ID = {0})", userEnteredId);

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

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