Reputation: 1301
What is the best way to split connection and executing query into two methods?
using System.Data.SQLite;
internal SQLiteConnection ConnectToDb()
{
using (SQLiteConnection dbConnection = new SQLiteConnection("@"DB\SWDB.db;"))
{
dbConnection.Open();
}
return dbConnection;
}
internal Qruery()
{
using (SQLiteCommand cmd = new SQLiteCommand(query, dbConnection))
{
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
SQLiteDataReader reader = cmd.ExecuteReader();
......
}
dbConnection.Close();
}
Since both objects are disposable, dbConnection is destroyed after using is done, so query cannot be made.
Upvotes: 0
Views: 35