I have tried to retrieve data from my table on MySql using my C# application. So I applied the usual connection methods in order to connect my c# application to my MySql database and also called to appropriate methods to retrieve the data from the table and then display it on my application. However, I noticed that by just using the following code :
conString = "server=localhost;user id=" + user + ";database=db;password="+pass;
connection = new MySqlConnection(conString);
DataTable table = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM users", connection);
adapter.Fill(table);
dataGridView1.DataSource = table;
I can retrieve the data from the table and display, without using the following code:
connection.Open();
what is the purpose to use connection.Open()
if I only need the following code to retrieve data? When will I need to use connection.Open()
?
Do I need to use connection.Open()
only when I sending information from my application to mysql but when I want to get/retrieve information from MySql then I don't need to use connection.Open()
, is this correct?