How connect your application to a Database? (C#)

1. Make a new project named "databaseConnect"

2. Drag and drop a button
3. Name it as "Connect"

4. You have to add a reference to your project. To do that go to solution explorer, right click on Reference , Add reference
5. Search for a "MySql.Data"
6. After find that tick it and click "OK"
7. The reference file will be added.
8. Import the following libery
using MySql.Data.MySqlClient;
8. Double click on the button & enter codes given below.
try
            {
                string myConnection = "datasource=localhost;port=3306;username=root;password=root";
                MySqlConnection myconn = new MySqlConnection(myConnection);
                MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
                myDataAdapter.SelectCommand = new MySqlCommand("select * databaseName.tableName;", myconn);
                MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
                myconn.Open();
              
                MessageBox.Show("Connected");
                myconn.Close();
            }
catch (Exception ex)
     {
                MessageBox.Show(ex.Message);
      }



9. Now check your connection. If it is success "Connected" Message will be show.

Next time we will see how add data to database?

1 comment:

  1. Thanks For sharing this Superb article.I use this Article to show my assignment in college.it is useful For me Great Work. best chair for programmers

    ReplyDelete