V
V
v1z2010-11-06 01:45:50
SQLite
v1z, 2010-11-06 01:45:50

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"];
        }


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

- You need to connect to the database when you open the program. As I understand it, the first line is responsible for this (ObjConnection = new SQLiteConnection). Where is the best place to move it?
- Where and how is it better to store this connection to the database so that I can always access it (something like a global variable)?
- How can I make a query that 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")
- The SQLite tab appeared in the elements panel with the elements Connection, DataAdapter, Command - what are they for? To visually adjust the base through them without writing it in the code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
andrew_kane, 2010-11-06
@v1z

>> 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 question

Ask a Question

731 491 924 answers to any question