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?