Answer the question
In order to leave comments, you need to log in
How to display table from database to datagrid?
Connecting to the database is done like this:
public static MySqlConnection connection = new MySqlConnection();
public static MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
public static void EstablishConnection()
{
builder.Server = "***.***.*.**";
builder.UserID = "student";
builder.Password = "student";
builder.Database = "recipesbook";
connection = new MySqlConnection(builder.ToString());
}
string sql = "SELECT name, amount, time from ListOfProducts";
using(MySqlConnection connection = new MySqlConnection(SQLconnect.builder.ToString()))
{
connection.Open();
using(MySqlCommand command = new MySqlCommand(sql, connection))
using (MySqlDataReader dataReader = command.ExecuteReader())
{
if(dataReader.HasRows)
{
while(dataReader.Read())
{
name = dataReader.GetString(0);
amount = dataReader.GetInt32(1);
time = dataReader.GetInt32(2);
}
}
else
{
MessageBox.Show("no rows");
}
}
connection.Close();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question