当Dapper不是对象时,如何使用Dapper正确声明SQL更新请求中的变量?

问题描述 投票:-1回答:1

我想这样做:

public int UpdateOneColumn(string dbName, string tableName, string columnName, string newValue, string whereColumnName, string whereColumnNameValue)
{
    string sql = @"update @tableName set @columnName = @newValue where @whereColumnName = @whereColumnNameValue";

    return connection.Execute(sql, new {tableName, columnName, newValue, whereColumnName, whereColumnNameValue });    
}

但是我收到了一个错误

必须声明tableName

有人知道如何正确声明我的变量“tableName,columnName,newValue,whereColumnName和whereColumnNameValue”?

这个功能是否正确? (我不确定我能做update @tableNamewhere @whereColumnName

c# sql-update dapper execute
1个回答
2
投票
public int UpdateOneColumn(string dbName, string tableName, string setColumn, object setValue, string whereColumn, object whereValue)
{
    string sql = $"UPDATE {tableName} SET {setColumn} = @s WHERE {whereColumn} = @w";
    return connection.Execute(sql, new { s = setValue, w = whereValue });    
}
© www.soinside.com 2019 - 2024. All rights reserved.