PHP 에서 Mysql 의 count 함수를 사용할때


 

1
2
3
4
$sql = "select count(컬럼)  from 테이블 where 조건문";
$row = mysql_fetch_row(mysql_query($sql));
 
$cnt = $row[0];


이런식으로 가장 많고 흔하게 이용이 됩니다. 쿼리문도 간결하고
나중에 덧붙여 사용할 수 있으니까 편하다고 할 수 있죠~

그런데 C# 에서 Mysql 의 count 함수를 사용할때는 조금 애매해 집니다.

그래서 그런지 C#에서 보통 아래처럼 사용하시는 분들이 간혹 계시더군요~


 

1
2
3
4
5
6
7
8
9
10
11
    MySqlDataReader cnt = null;
 
    MySqlCommand count = new MySqlCommand("select count(컬럼) cnt  from 테이블 where 조건문", DB연결);
 
    cnt = count.ExecuteReader();
 
    cnt.Read();
 
    int myCount = Convert.ToInt32(cnt["cnt"].ToString());
 
    cnt.Close();

 

물론 위의 방법도 그다지 나쁜 방법이라고는 할 수 없지만 나중에 Mysql 컬럼을 추가로 불러오게 된다면 쿼리문도
좀 길어지고 파싱도 귀찮아 질 듯 합니다.

그래서 나름 방법을 찾아보니 PHPmysql_fetch_row 와 비슷한(?) 방법이 존재하더군요~
OLEDB 에선 조금더 간결한 방법이 있는듯 한데 자세한건 모르겠네요~


 

1
2
 MySqlCommand count = new MySqlCommand("select count(컬럼)  from 테이블 where 조건문", DB연결);
 int myCount = Convert.ToInt32(count.ExecuteScalar());



요렇게 간단하게 끝낼수도 있더군요~


보다 자세한건

참조링크 : http://www.mysqlkorea.co.kr/sub.html?mcode=manual&scode=01&m_no=22646&cat1=23&cat2=700&cat3=727&lang=k

작성일 : 2010년 08월 30일 14:11

Posted by 애드의나날
: