2013-10-11 4 views
3

Oracle과 Petapoco를 사용하여 행의 존재를 얻으려고합니다. 지금까지 나는 다음 코드를 가지고있다.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') ;' –

답변

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'");