使用来自 C#/Dapper 的 optional/condition 参数更新 SQL Server
Update SqlSever with optional/condition parameters from C#/Dapper
我有一个应用程序,我从 API 中获取数据并将其发送到我的 SQL 服务器数据库。有时我只想用新数据从我的应用程序更新数据库中的某一列。
这是我运行在应用程序中立即发送数据的代码
public void TestinPE(long? instID, float? Value)
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("BorsdataDB")))
{
//Person newPerson = new Person { ModelName = Variable, LastName = lastName, EmailAddress = emailAddress, PhoneNumber = phoneNumber };
List<KpiReturn> updateKpi = new List<KpiReturn>();
updateKpi.Add(new KpiReturn { instID = instID, psValue = Value });
connection.Execute("dbo.UpdateRec @instID,@psValue", updateKpi);
}
}
我调用的过程如下所示
ALTER PROCEDURE [dbo].[UpdateRec]
@instID int,
@psValue float = null,
@peValue float = null
AS
BEGIN
SET NOCOUNT ON;
UPDATE StockData
SET peValue = ISNULL(@peValue, peValue),
psValue = ISNULL(@psValue, psValue)
WHERE instID = @instID
END
现在假设我想更新 psValue
它工作得很好,但是如果我改为尝试更新 peValue
,并且我将我的 C# 代码更改为
updateKpi.Add(new KpiReturn { instID = instID, peValue = Value });
connection.Execute("dbo.UpdateRec @instID,@peValue", updateKpi);
我的 SQL 服务器更新中的 psValue
,在这种情况下,它总是更新查询中的第一个参数 @psValue float = null
。
如果我 运行 在 SQL 中的查询,它工作正常,如下所示,它忽略 psValue
,但更新 peValue
。但是我可以在尝试从 C#
传递参数时实现这一点
exec dbo.UpdateRec @instID = 2, @peValue = 500
在这方面对我有什么提示吗?
It works fine if I run the query in SQL as shown below, it ignores
psValue, but updates peValue. But I can get this to happen when trying
to pass parameters from C#
exec dbo.UpdateRec @instID = 2, @peValue = 500
这些代码片段按序号而不是名称传递参数值,因此无论提供的参数名称如何,proc 代码都会将值视为 @instID
和 @psValue
:
updateKpi.Add(new KpiReturn { instID = instID, psValue = Value });
connection.Execute("dbo.UpdateRec @instID,@psValue", updateKpi);
updateKpi.Add(new KpiReturn { instID = instID, peValue = Value });
connection.Execute("dbo.UpdateRec @instID,@peValue", updateKpi);
修复它的一种方法是使用命名参数语法而不是序数方法,类似于在 SSMS 中编写 运行 脚本:
updateKpi.Add(new KpiReturn { instID = instID, psValue = Value });
connection.Execute("dbo.UpdateRec @instID=@instID, @psValue=@psValue", updateKpi);
updateKpi.Add(new KpiReturn { instID = instID, peValue = Value });
connection.Execute("dbo.UpdateRec @instID=@instID, @peValue=@peValue", updateKpi);
我有一个应用程序,我从 API 中获取数据并将其发送到我的 SQL 服务器数据库。有时我只想用新数据从我的应用程序更新数据库中的某一列。
这是我运行在应用程序中立即发送数据的代码
public void TestinPE(long? instID, float? Value)
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("BorsdataDB")))
{
//Person newPerson = new Person { ModelName = Variable, LastName = lastName, EmailAddress = emailAddress, PhoneNumber = phoneNumber };
List<KpiReturn> updateKpi = new List<KpiReturn>();
updateKpi.Add(new KpiReturn { instID = instID, psValue = Value });
connection.Execute("dbo.UpdateRec @instID,@psValue", updateKpi);
}
}
我调用的过程如下所示
ALTER PROCEDURE [dbo].[UpdateRec]
@instID int,
@psValue float = null,
@peValue float = null
AS
BEGIN
SET NOCOUNT ON;
UPDATE StockData
SET peValue = ISNULL(@peValue, peValue),
psValue = ISNULL(@psValue, psValue)
WHERE instID = @instID
END
现在假设我想更新 psValue
它工作得很好,但是如果我改为尝试更新 peValue
,并且我将我的 C# 代码更改为
updateKpi.Add(new KpiReturn { instID = instID, peValue = Value });
connection.Execute("dbo.UpdateRec @instID,@peValue", updateKpi);
我的 SQL 服务器更新中的 psValue
,在这种情况下,它总是更新查询中的第一个参数 @psValue float = null
。
如果我 运行 在 SQL 中的查询,它工作正常,如下所示,它忽略 psValue
,但更新 peValue
。但是我可以在尝试从 C#
exec dbo.UpdateRec @instID = 2, @peValue = 500
在这方面对我有什么提示吗?
It works fine if I run the query in SQL as shown below, it ignores psValue, but updates peValue. But I can get this to happen when trying to pass parameters from C#
exec dbo.UpdateRec @instID = 2, @peValue = 500
这些代码片段按序号而不是名称传递参数值,因此无论提供的参数名称如何,proc 代码都会将值视为 @instID
和 @psValue
:
updateKpi.Add(new KpiReturn { instID = instID, psValue = Value });
connection.Execute("dbo.UpdateRec @instID,@psValue", updateKpi);
updateKpi.Add(new KpiReturn { instID = instID, peValue = Value });
connection.Execute("dbo.UpdateRec @instID,@peValue", updateKpi);
修复它的一种方法是使用命名参数语法而不是序数方法,类似于在 SSMS 中编写 运行 脚本:
updateKpi.Add(new KpiReturn { instID = instID, psValue = Value });
connection.Execute("dbo.UpdateRec @instID=@instID, @psValue=@psValue", updateKpi);
updateKpi.Add(new KpiReturn { instID = instID, peValue = Value });
connection.Execute("dbo.UpdateRec @instID=@instID, @peValue=@peValue", updateKpi);