当 Dapper 不是对象时,如何在 SQL 更新请求中正确声明变量?
How correctly declare variables in SQL update request with Dapper when it isn't an object?
我想做这个请求:
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 must be declared
有人知道如何正确声明我的变量 "tableName, columnName, newValue, whereColumnName and whereColumnNameValue" 吗?
这个函数正确吗? (我不确定我能做到 update @tableName
和 where @whereColumnName
)
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 });
}
我想做这个请求:
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 must be declared
有人知道如何正确声明我的变量 "tableName, columnName, newValue, whereColumnName and whereColumnNameValue" 吗?
这个函数正确吗? (我不确定我能做到 update @tableName
和 where @whereColumnName
)
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 });
}