如何從數據庫中獲取表的總數量? 我的代碼如下:SQLite從數據庫中獲取表的數量
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
}
謝謝Jigar,你的代碼工作。另外,我不需要聲明對象結果(雖然這對我來說更清楚)。它也可以直接說int nTables = Convert.ToInt32(cmd.ExecuteScalar()); –