Soheyl Soheyl - 1 year ago 105
MySQL Question

How run a C# query?

I use Visual Studio community 2015 and i use MySQL connector (MySQL for Visual Studio) for connection my MySQL database to Visual Studio, this part is already done and i have Visual Studio connected to database.

Now i like to know what is my next step to get (using a select query) data from the database to my form program ?

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 Test_1
public partial class Form1 : Form
public Form1()

private void Form1_Load(object sender, EventArgs e)
// How can i get data from the database in here ?

I got my answer ! Check best answer.

Answer Source

I got my answer :

MySqlConnection sqlConnection1 = new MySqlConnection("server=server;uid=username;" + "pwd=password;database=database;");
MySqlCommand cmd = new MySqlCommand();
MySqlDataReader reader;

cmd.CommandText = "SELECT * FROM table";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

reader = cmd.ExecuteReader();

 value = reader.GetString(x);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download