Also you should have the knowledge of MySQL localhost and its users privileges. Please see the process below.
|
|
How To Connect MySQL To Visual Studio
- Download and install all the pre-requistics items mentioned above. Note: Please note down the username & password of MySQL to use it Visual Studio.
- Open Visual Studio and Add a new project.
- Now go to solution explorer -> Click on References -> Add Reference
- Now Click on Browse and navigate through this location "C:\Program Files (x86)\MySQL\MySQL Connector Net 5.0.9\Binaries\.NET 2.0" and click on MySql.Data.dll file -> Click Add
- Now Add the following namespace to your newly created project. This namespace helps to use MySQL Data Instances - (using MySql.Data.MySqlClient)
- Please use the below code to connect to mysql and enter your queries as required.
private MySqlConnection Connect()
{
string MyConnectionString = "Server=localhost;Database=networkcop;Uid=networkcopuser;Pwd=network1234";
MySqlConnection connection = new MySqlConnection(MyConnectionString);
return connection;
}
private void Disconnect(MySqlConnection connection)
{
connection.Close();
}
private void button1_Click(object sender, EventArgs e)
{
MySqlConnection con = Connect();
con.Open();
MySqlCommand cmd;
cmd = con.CreateCommand();
cmd.CommandText = "Select * from useractivity";
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
blockurlsdatagrid.Visible = true;
blockurlsdatagrid.DataSource = ds.Tables[0].DefaultView;
}
- Enjoy!! and let me know if you have queries.