3
Reply

c# using mysql plus return data

Vergel Aranas

Vergel Aranas

Mar 7 2015 4:21 PM
595
Hi I have a problem in retrieving data from mysql and return data.
This the scenario for details.
I have a class called database then under that class i have function class public string listh() so that i can return a data to anywhere that will instantiate and call this method. I want to make this function dynamic. That's why i separate this. the query is good no errror but the problem is how to return and foreach data so that i can put it into datagridview . My code is this. 
 
public class database { 
public string list() {
confirm = db_conn.Cs;
try
{
conn = new MySqlConnection(confirm);
conn.Open();
string query = "SELECT * FROM palma_user";
command = new MySqlCommand(query, conn);
dr = command.ExecuteReader();
return dr.ToString();
 
}
catch (MySqlException ex)
{
return ex.ToString();
}
finally {
if(conn != null){
conn.Close();
}
}
}
so if is instantiated something like this.
 
Database db = new Database();
db.list();
 
with that, i want to loop something like this.
 
while (dr.Read())
{
   dr.GetString(1));
}
 
that code is good only if the the query is not separated from the Form or let us direct loop and put into grid.
In my case i separate the code from the class and return the value.
Please help me. Thanks.
 
Regards,
Vergel 
 
 
 

Answers (3)