Simple reading data from MySQL


This example is the most basic. We are just using a single select * from city to fill our dataset:



        public DataTable GetData_MySQL()
        {
            MySql.Data.MySqlClient.MySqlConnection oConn = new MySql.Data.MySqlClient.MySqlConnection();
            oConn.ConnectionString = "Database=sakila;Data Source=192.168.10.4;User id=stefan;Password=pekka";
            oConn.Open();

            DataSet oDataSet = new System.Data.DataSet("tab1");
            //Get data
            MySql.Data.MySqlClient.MySqlCommand command =
                new MySql.Data.MySqlClient.MySqlCommand("select * from city", oConn);

            MySql.Data.MySqlClient.MySqlDataAdapter oAdapter =
                new MySql.Data.MySqlClient.MySqlDataAdapter(command);
            oAdapter.Fill(oDataSet, "tab1");
            DataTable dt = oDataSet.Tables["tab1"];
            oConn.Close();

            return dt;

            
        }

In my testapp (Windows Form) I have a gatagrid which I set to the result:

dataGridView1.DataSource = GetData_MySQL();