开发者

how get all records on database using mysql?

开发者 https://www.devze.com 2023-04-01 22:00 出处:网络
I tried this: MySqlConnection con = new MySqlConnection(...); con.Open(); MySqlCommand cmd = new MySqlCommand();

I tried this:

  MySqlConnection con = new MySqlConnection(...);
    con.Open();
        MySqlCommand cmd = new MySqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT * FROM questions;";
        MySqlDataReader reader = cmd.ExecuteReader();
        reader.Read();
        int i = 0, len = reader.FieldCount;
        while (i < len)
        {
            Response.Write(reader.GetString(i));
            i++;
        }

returns only the first values from table. how get all? thanks in advance开发者_StackOverflow


You have to call reader.Read() until it returns false.

I've also taken the liberty of converting your inner loop to a for loop.

while (reader.Read()) 
{
    for (int i = 0; i < reader.FieldCount; i++)
    {
        Response.Write(reader.GetString(i));
    }
}

Read this to read up on the IDataReader : http://msdn.microsoft.com/en-us/library/system.data.idatareader.read.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消