C# KONEKSI MYSQL QUERY TO DATATABLE (ADAPTER) SAMPLE CODE

   

KONEKSI MYSQL TO DATATABLE – Kali ini saya akan memberikan koneksi data ke MySql untuk mendapatkan data (select) ke Aplikasi yang kita buat (Website / Dekstop), berikut script nya:

public static DataTable QueryToDataTable(string Query, MySqlConnection connection)
        {
            DataTable dt = new DataTable();
            try
            {
                connection.Open();
                MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter();
                MySqlCommand cmd = new MySqlCommand(Query, connection);
                DataSet dataSet = new DataSet();
                mySqlDataAdapter.SelectCommand = cmd;
                mySqlDataAdapter.Fill(dataSet);
                dt = dataSet.Tables[0];
                connection.Close();
            }
            catch (Exception Ex)
            {
                _Msg.MessageShow_Err(Ex.Message);
            }
            return dt;
        }

_Msg.MessageShow_Err(Ex.Message) : Merupakan fungsi yang saya buat untuk menampilkan message error sama seperti : MessageBox.Show(Ex.Message).

Wahyu Amaldi, M.Kom has written 87 articles

Hi Reader, thank you for read this article, i hope this article can be useful.
If you will share knowladge to me, dont forget contact to me.

Email 1 : wahyu.amaldi@outlook.com
Email 2 : wahyu.amaldi@ilmuprogram.com

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>