Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.0 kB
4
Indexable
Never
using System.Data;

namespace SF_Aguila_285
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string ConnString = @"SERVER=localhost;
                              DATABASE=tinycollegelibrary;
                              UID=root;
                              PWD=;";

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
                //1. Connect with Database
                MySqlConnection Conn = new MySqlConnection(ConnString);
                string sSQL = "";
                DataTable dt = new DataTable(); //Hold records from table
                try
                {
                    Conn.Open(); //Establish connection with MySql

                    //2. Setup command
                    MySqlCommand cmd = new MySqlCommand();
                    cmd.Connection = Conn;
                    cmd.CommandType = CommandType.Text;

                    sSQL = "SELECT * FROM tblbooks"; //SQL Query
                    cmd.CommandText = sSQL;

                    //3. Data Reader
                    MySqlDataReader dr = cmd.ExecuteReader();

                    dt.Load((IDataReader)dr); //Transfer DataReader(dr) data to DataTable(dt)
                    dataGridView1.DataSource = dt; //Show data into DataGridView
                    Conn.Close();
                }
                catch
                {
                    MessageBox.Show("Connection Error!");
                    return;
                }
            }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            txtISBN.Text = "";
            txtTitle.Text = "";
            txtAuthor.Text = "";
            LoadData();
        }

        private void LoadData()
        {
            throw new NotImplementedException();
            
        }
    }
  }
Leave a Comment