protected override async void OnAppearing() {
dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "OrderappDb.db");
bool DbExists = File.Exists(dbPath);
var connect = new SqliteConnection("Data Source=" dbPath);
private bool tableExists(SqliteConnection connection, string tableName)
{
dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "OrderappDb.db");
var connect = new SqliteConnection("Data Source=" dbPath);
connection = connect;
try
{
if (connection.State == System.Data.ConnectionState.Closed)
connection.Open();
using (cmd = new SqliteCommand(connection.ToString()))
{
//sqlite_master
cmd.CommandText = $"Select Count(*) from sqlite_master where type='table' and name='{tableName}';";
object result = cmd.ExecuteScalar();
//connection.Open();
int resultCount = Convert.ToInt32(result);
if (resultCount > 0)
return true;
connection.Close();
// sqlite_master
}
return false;
}
catch (Exception ex)
{
return false;
}
}
I am Creating table in sqlite and aceesing that table. Getting ,ExecuteScalar can only be called when the connection is open.Why I Am Getting above error dont understand. Please help with my snippet.
CodePudding user response:
There is not an overload for SqliteCommand
how you are using it. You'll want to use
Using SqliteCommand cmd = new SqliteCommand(yourSqlString, connection) {
Integer val = cmd.executeScalar();
}
CodePudding user response:
The SqlCommand.ExecuteReader
Method Sends the CommandText
to the Connection and builds a SqlDataReader
. So it need connection.Open();
method to open the connection and this is why the ExecuteScalar
method can only be called when the connection open.