using AR;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UniMarc
{
    public partial class fDevDB : Form
    {
        DataTable dt = new DataTable();
        Helper_DB db = new Helper_DB();
        public fDevDB()
        {
            InitializeComponent();
        }

        private void fDevDB_Load(object sender, EventArgs e)
        {
            if (this.cmbTables.Items.Count > 0)
                this.cmbTables.SelectedIndex = 0;
        }

        private void btsave_Click(object sender, EventArgs e)
        {
            //save
            var sql = $"select * from {cmbTables.Text}";
            var cn = db.CreateConnection();
            var da = new MySql.Data.MySqlClient.MySqlDataAdapter(sql, cn);
            var cb = new MySql.Data.MySqlClient.MySqlCommandBuilder(da);
            da.UpdateCommand = cb.GetUpdateCommand();
            da.InsertCommand = cb.GetInsertCommand();
            da.DeleteCommand = cb.GetDeleteCommand();
            var cnt = da.Update(dt);
            UTIL.MsgI($"P{cnt}");
            cn.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //query
            var sql = $"select * from {cmbTables.Text}";
            var cn = db.CreateConnection();
            var da = new MySql.Data.MySqlClient.MySqlDataAdapter(sql, cn);

            da.Fill(dt);
            this.bs1.DataSource = dt;
            this.bn1.BindingSource = this.bs1;
            this.dv1.DataSource = this.bs1;

            cn.Dispose();
        }
    }
}
