데이터베이스에서 총 테이블 수를 어떻게 얻을 수 있습니까? 내 코드는 다음과 같습니다.
string sDatabasePath = DBPath();
SQLiteConnectionStringBuilder datasource = new SQLiteConnectionStringBuilder();
datasource.Add("Data Source", sDatabasePath);
datasource.Add("Version", "3");
datasource.Add("New", "False");
datasource.Add("Compress", "True");
//int nTables;
using (SQLiteConnection connection = new SQLiteConnection(datasource.ConnectionString))
{
connection.Open(); //opens connection
SQLiteCommand command = new SQLiteCommand("Select Count(*) FROM sqlite_master where type='table' as nTables;");
//SqlCommand comm = new SqlCommand("SELECT COUNT(*) FROM sqlite_master where type='table'", connection);
Int32 nTables = (Int32) comm .ExecuteScalar(); //this query raises error Specified cast is not valid.
connection.Close(); // closes the connection
}
ExecuteScalar () 메서드는 단일 값을 반환합니다. 이것은 계산 결과를 반환하는 데 사용됩니다. COUNT 요청 등.
cmd.ExecuteScalar();
반환 된 값은 결과의 Object입니다.
당신이 할 수있는 것은 Int32로 변환하기 위해 다음과 같은 방법으로 시도하는 것입니다.
object result = cmd.ExecuteScalar();
int nTables = Convert.ToInt32(result);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다