您好我正在使用linq和sql来创建一个窗口serrvice更新一些行,如果在过去的5分钟内迄今表中有变化,这是什么我有:从2个不同的数据库,2个不同的表Linq到SQL bulkupdate
using System;
using System.Data.SqlClient;
using System.Linq;
namespace Prueba
{
internal static class Program
{
private static void Main()
{
int tienda = 9;
var conex = new DataClasses1DataContext();
try
{
var source =
new SqlConnection(
"Server=LAPTOP-VCD9V9KH\\SQLEXPRESS;Database=backoffice;User Id=sa; Password=root;");
var destination =
new SqlConnection(
"Server=LAPTOP-VCD9V9KH\\SQLEXPRESS;Database=Corporativo_PRB;User Id=sa; Password=root;");
source.Open();
destination.Open();
source.CreateCommand();
var infoExistenciases = conex.Info_Existencias.Where(x => x.FechAct > DateTime.Now.AddMinutes(-5));
foreach (var x in infoExistenciases)
{
var cmd2 = new SqlCommand(
"update Info_Corp_Existencias set Existencia =" + x.Existencia + " where sku ='" + x.SKU + "'" +
"AND Tienda =" + tienda, destination);
cmd2.ExecuteNonQuery();
}
source.Close();
destination.Close();
Console.Beep();
}
catch
(Exception e)
{
Console.WriteLine(e);
Console.ReadLine();
throw;
}
}
}
}
到目前为止,该代码更新目标服务器的一切,但我真的不能真正找准下来怎么把一切到一个临时表,让LINQ更新它的foreach参数。
谢谢您的时间
因此,不是运行一个更新语句,而是想要使用linq并为每一行执行update-statement? –
是的,目前它为每个语句都这样做,但我需要生成一些批量使用一个连接到数据库,而不是使用foreach属性。 – Darkpaladin