SQL 쿼리에서 결과 캡처: 카운트 값 검색
SQL 쿼리의 카운트 값을 C#의 정수 변수로 쉽게 검색하려면 , SqlCommand.ExecuteScalar() 메서드를 활용하세요. 이 메소드는 지정된 쿼리를 실행하고 첫 번째 열의 첫 번째 행 값을 반환합니다.
구현:
이 기술을 사용하려면 다음 단계를 따르세요.
예 코드:
using System; using System.Data; using System.Data.SqlClient; class Program { static void Main() { string connectionString = @"Data Source=(local);Initial Catalog=myDatabase;Integrated Security=SSPI;"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM table_name", connection); Int32 count = (Int32)cmd.ExecuteScalar(); Console.WriteLine($"The count of records in table_name is {count}"); } } }
이 기술을 활용하면 다음에서 카운트 값을 편리하게 캡처하고 저장할 수 있습니다. C# 코드의 SQL 쿼리.
위 내용은 C#에서 SQL 카운트 값을 정수로 검색하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!