C# + sqlite, a few questions


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
8 views
In my previous question I asked about the language, and so decided to start writing a program in C#

But since the language is new to me immediately raised some questions. Some have decided with the help of Google, but the database has the problem.

Connect to sqlite using ADO.NET (http://sqlite.phxsoftware.com/)
Using VS to create the users table, fill it with some info to take it out.
Throw on the form the GridView.
Then, using this guide, 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 = the dataSet.Tables["users"]; }


And everything works fine, when loading the program show data from the database. But that's not it. I want to do a little refactoring:

— You need to establish a connection with the database when you open the program. As I understand it, is responsible for the first line (ObjConnection = new SQLiteConnection). Where is better to move?
— Where and how to store the connection with the database, so I can always access it (something like global variable)?
— How do I make a query that will pull one line, to continue to work with her? You need something like ObjConnection.query("SELECT login FROM users WHERE id = 1")
— In the Toolbox tab has been added SQLite with elements of the Connection, DataAdapter, Command, — what are they for? To visually configure the database through them, not writing it in code?
by | 8 views

1 Answer

0 like 0 dislike
>> You need to establish a connection with the database when you open the program. As I understand it, is responsible for the first line (ObjConnection = new SQLiteConnection). Where is better to move?
Generally for such purposes creates a data access layer separate from the presentation layer, which encapsulates all the logic for managing database queries (Google "n-tier application"). Constantly keep open one connection — not the best option. But unfortunately, I can't say how things are going with caching of connections in this sqlite-provider, it is necessary to test.
\r
>> Where and how to store the connection with the database, so I can always access it (something like global variable)?
For desktop applications the standard configuration file app.config (set values for the connection string through Project Settings -> Settings, entries specify scope = Application, then its value is accessible using %namespace%.Properties.Settings)
\r
>> How do I make a query that will pull one line, to continue to work with her? You need something like ObjConnection.query("SELECT login FROM users WHERE id = 1")
\rExecuteReader()
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
4 answers
asked Jun 8, 2019 by baimkin
110,608 questions
257,186 answers
0 comments
25,712 users