Answer the question
In order to leave comments, you need to log in
C# + sqlite, a few questions
In my previous question , I asked about the language, and so I decided to start writing a program in C#.
But since the language is new to me, some questions immediately arose. I solved some with the help of Google, but there was a problem with the database.
I connect sqlite via ADO.NET (http://sqlite.phxsoftware.com/)
Through VS I create a users table, fill in some information there to display it.
I throw a GridView element on the form.
Then using this guide , I write:
private void Form1_Load(object sender, EventArgs e)
{
SQLiteConnection ObjConnection = new SQLiteConnection("Data Source=data/database.db3;");
SQLiteCommand ObjCommand = new SQLiteCommand("SELECT * FROM users", ObjConnection);
ObjCommand.CommandType = CommandType.Text;
SQLiteDataAdapter ObjDataAdapter = new SQLiteDataAdapter(ObjCommand);
DataSet dataSet = new DataSet();
ObjDataAdapter.Fill(dataSet, "users");
dataGridView1.DataSource = dataSet.Tables["users"];
}
Answer the question
In order to leave comments, you need to log in
>> Need to connect to the database when opening the program. As I understand it, the first line is responsible for this (ObjConnection = new SQLiteConnection). Where is the best place to move it?
In general, a data access layer is usually created for such purposes, separate from the presentation layer, which encapsulates all the logic for managing requests to the database (google “n-tier application”). Keeping one connection always open is not the best option. But unfortunately, I can’t say how things are with connection caching in this sqlite provider, it’s already necessary to test here.
>> Where and how best to store this database connection so that I can always access it (something like a global variable)?
For desktop applications - a standard configuration file app.config (you can set values for connection strings through Project Settings -> Settings, specify scope = Application for the entry, then its value is available through %namespace%.Properties.Settings)
>> How can I make a request , which will pull out one line in order to work with it in the future? You need something like ObjConnection.query("SELECT login FROM users WHERE id = 1")
ExecuteReader()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question