I'm trying to display data from a MySQL table onto a webpage and would like to iterate through each row using razor (and/or .NET). So far I've successfully connected to the database but I would then like to create a foreach loop (or the equivalent) so that I can render out the table data in the desired format and retrieve the different columns of each row.
Here is my current code, where the foreach causes the script to error ("'MySql.Data.MySqlClient.MySqlConnection' does not contain a definition for 'Query'"):
@using MySql.Data.MySqlClient; @{ MySqlConnection con = new MySqlConnection("server=localhost;database=database;user id=user;password=password"); con.Open(); string sql = "SELECT * FROM table"; MySqlCommand cmd = new MySqlCommand(sql, con); @foreach(var row in con.Query(sql)) { <ul> <li>@row.name - @row.description</li> </ul> } con.Close(); }
Is this possible to do in Umbraco without the aid of premium plugins? What needs to replace the foreach loop?
I don't think it's anything to do with Umbraco - the error is stating that your con variable (a MySqlConnection object) does not have a method called Query.
Iterate through MySQL table data with razor
Hi All,
I'm trying to display data from a MySQL table onto a webpage and would like to iterate through each row using razor (and/or .NET). So far I've successfully connected to the database but I would then like to create a foreach loop (or the equivalent) so that I can render out the table data in the desired format and retrieve the different columns of each row.
Here is my current code, where the foreach causes the script to error (" 'MySql.Data.MySqlClient.MySqlConnection' does not contain a definition for 'Query' "):
Is this possible to do in Umbraco without the aid of premium plugins? What needs to replace the foreach loop?
Kind Regards,
mmmoustache
Hi,
I don't think it's anything to do with Umbraco - the error is stating that your con variable (a MySqlConnection object) does not have a method called Query.
Are you sure that's a valid method?
This might be another way to do it.
Thanks,
Tom
Okay, thought that might be the case. Cheers for the info anyway Tom.
is working on a reply...