Soheyl Soheyl - 26 days ago 16
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()
{
InitializeComponent();
}

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

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;

sqlConnection1.Open();
reader = cmd.ExecuteReader();

try
{
 reader.Read();
 value = reader.GetString(x);
}
finally
{
 reader.Close();
}