使用Oracle和Petapoco获取行存在的最佳方法

3

我正在尝试使用Oracle和Petapoco获取行的存在性。到目前为止,我有以下代码。

var sql =new Sql("select COUNT(*) FROM myTable where field = 'value'");
var exists = myDB.Query<int>(sql) > 0;

我感觉这样做有点不太好,因为我把工作分散在了数据库和我的应用程序中。有没有一种方法可以像下面这样做呢?

var exists = myDB.Query<bool>(someNewSqlThatReturnsBool);

2
很遗憾,我认为这是不可能的。然而,我建议采用稍微不同(在我看来更好)的方法编写查询以检查记录是否存在:SELECT COUNT(1) FROM dual WHERE EXISTS (SELECT 1 FROM yourTable WHERE field = 'value'); - Przemyslaw Kruglej
1个回答

3
使用PetaPoco,您应该能够使用API中如下所示的Exists方法重载。
/// <summary>
/// Checks for the existance of a row matching the specified condition
/// </summary>
/// <typeparam name="T">The Type representing the table being queried</typeparam>
/// <param name="sqlCondition">The SQL expression to be tested for (ie: the WHERE expression)</param>
/// <param name="args">Arguments to any embedded parameters in the SQL statement</param>
/// <returns>True if a record matching the condition is found.</returns>
public bool Exists<T>(string sqlCondition, params object[] args) 

所以您应该可以直接调用:
var exists = myDB.Exists<myTable>("field = 'value'");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接