Karthik Malla
Karthik Malla

Reputation: 5820

ODBC Select all command

The following code works fine when the limit is 0,1 when I limit them to 0,30 I cannot retrieve records I am sure that my database has enough number of records. But I doubt the syntax after dr[3].ToString(); How to retrieve all of them.

string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=malla_softmail2;" + "UID=xxx;" + "PASSWORD=xxx;" + "OPTION=3";
        OdbcConnection MyConnection = new OdbcConnection(MyConString);
        MyConnection.Open();
        OdbcCommand cmd = new OdbcCommand("Select * from awm_test where user=? limit= 0, 1", MyConnection);
        cmd.Parameters.Add("@email", OdbcType.VarChar, 255).Value = "hello";
        OdbcDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows == false)
        {
          //  throw new Exception();
        }
        if (dr.Read())
        {
            string a = dr[0].ToString();
            string b = dr[1].ToString();
            string c = dr[2].ToString();
            //string d = dr[3].ToString();
            //string f = dr[4].ToString();
            //string g = dr[5].ToString();

            Response.Write(a);
            Response.Write(b);
            Response.Write(c);
            //Response.Write(d);
            //Response.Write(f);
            //Response.Write(g);

Upvotes: 1

Views: 1397

Answers (2)

Will A
Will A

Reputation: 25008

You just need to read the rows in a loop with while(dr.Read()) {...do stuff for each row...} - each dr.Read() moves to the next row.

Upvotes: 0

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263067

You're only processing the first record returned by your query. Try:

while (dr.Read()) {
    string firstField = dr[0].ToString();
    string secondField = dr[1].ToString();
    string thirdField = dr[2].ToString();

    // ...
}

Upvotes: 2

Related Questions