string cmd = @"IF NOT EXISTS (SELECT * FROM Table1 WHERE SupplierId=@SupplierId AND InvoiceId=@InvoiceId)
BEGIN
INSERT INTO Table1(SupplierId, InvoiceId) VALUES(@SupplierId, @InvoiceId)
END";
using (var dbConn = new SqlConnection("Server=(local);Database=MyDatabase;Integrated Security=true"))
{
dbConn.Execute(cmd, new { SupplierId = 1, InvoiceId = 2 });
}
Console.ReadKey();
更新mysql,你可以写 INSERT INTO Table1 (SupplierId, InvoiceId) VALUES(@SupplierId, @InvoiceId) ON DUPLICATE KEY SupplierId=SupplierId https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
1条答案
按热度按时间jpfvwuh41#
您可以简单地先执行select查询,然后再执行insert,也可以在一次行程中执行,如下所示(SQLServer)。您可以使用此处列出的推荐dapper扩展之一
更新mysql,你可以写
INSERT INTO Table1 (SupplierId, InvoiceId) VALUES(@SupplierId, @InvoiceId) ON DUPLICATE KEY SupplierId=SupplierId
https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html