반응형
SQL 조회에서 카운트 캡처
C#(.cs 파일)에서 SQL 명령에서 카운트를 가져오는 가장 간단한 방법은 무엇입니까?
SELECT COUNT(*) FROM table_name
의 상태로.int
변수?
SqlCommand를 사용합니다.Scalar()를 실행하고 다음으로 캐스트합니다.int
:
cmd.CommandText = "SELECT COUNT(*) FROM table_name";
Int32 count = (Int32) cmd.ExecuteScalar();
SqlConnection conn = new SqlConnection("ConnectionString");
conn.Open();
SqlCommand comm = new SqlCommand("SELECT COUNT(*) FROM table_name", conn);
Int32 count = (Int32) comm .ExecuteScalar();
변환 오류는 다음과 같이 표시됩니다.
cmd.CommandText = "SELECT COUNT(*) FROM table_name";
Int32 count = (Int32) cmd.ExecuteScalar();
대신 사용:
string stm = "SELECT COUNT(*) FROM table_name WHERE id="+id+";";
MySqlCommand cmd = new MySqlCommand(stm, conn);
Int32 count = Convert.ToInt32(cmd.ExecuteScalar());
if(count > 0){
found = true;
} else {
found = false;
}
SQL로 C# 보완:
SqlConnection conn = new SqlConnection("ConnectionString");
conn.Open();
SqlCommand comm = new SqlCommand("SELECT COUNT(*) FROM table_name", conn);
Int32 count = Convert.ToInt32(comm.ExecuteScalar());
if (count > 0)
{
lblCount.Text = Convert.ToString(count.ToString()); //For example a Label
}
else
{
lblCount.Text = "0";
}
conn.Close(); //Remember close the connection
int count = 0;
using (new SqlConnection connection = new SqlConnection("connectionString"))
{
sqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM table_name", connection);
connection.Open();
count = (int32)cmd.ExecuteScalar();
}
언급URL : https://stackoverflow.com/questions/4668911/capturing-count-from-an-sql-query
반응형
'programing' 카테고리의 다른 글
Wordpress wp_editor 텍스트 영역에 사용자 지정 속성을 추가하는 방법은 무엇입니까? (0) | 2023.07.06 |
---|---|
Spring mongo 데이터 저장소 인터페이스를 위한 "16진수 표현 추가" (0) | 2023.07.06 |
WebFlux 기능:빈 플럭스를 감지하고 404를 반환하는 방법은 무엇입니까? (0) | 2023.07.06 |
공동작업자를 Firebase 앱에 추가하는 방법은 무엇입니까? (0) | 2023.07.06 |
오라클 SQL에서 join 키워드와 inner join 키워드의 차이점은 무엇입니까? (0) | 2023.07.06 |