Local DB Connection and Read

    0

    0

    monsta developing

             dataGridView1.Update();
             }
    

    In this C# code, a database connection is created and a MySql.Data.MySqlClient.MySqlCommand object is created. The MySql.Data.MySqlClient.MySqlCommand object contains the commands to be executed against the database. The MySql.Data.MySqlClient.MySqlDataAdapter object is used to Fill the DataTable object with the results of the commands. Finally, the MySql.Data.MySqlClient.MySqlConnection object is closed.

    DatabaseConnection dc = new DatabaseConnection();
      MySql.Data.MySqlClient.MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("datasource = 127.0.0.1;username=root;password=;database=customer");
      MySql.Data.MySqlClient.MySqlCommand command;
      MySql.Data.MySqlClient.MySqlDataAdapter da;
      DataTable dt;
    
    private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    con.Open();
                    command = new MySql.Data.MySqlClient.MySqlCommand("Select * From customers",con);
                    command.ExecuteNonQuery();
                    dt = new DataTable();
                    da = new MySql.Data.MySqlClient.MySqlDataAdapter(command);
                    da.Fill(dt);
                    dataGridView1.DataSource= dt;
                    con.Close();
                }catch(Exception ex)
                {
    
                }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.