简体   繁体   中英

Returning 2-dimensional array via C#

I'm getting content from a database and returning it for Ajax processing via Javascript. Pretty simple stuff. The issue here is that I can't seem to find a good way to loop through the data and the MSDN documentation is obscenely poor for its odbcreader methods.

using (OdbcCommand com = new OdbcCommand("SELECT * FROM pie_data WHERE Pie_ID = ?",
 con)) {
  if (Request.Form["reference_id"] == "") {
    returnError();
  } else {
    com.Parameters.AddWithValue("", Request.Form["reference_id"]);
    com.ExecuteNonQuery();
    using (OdbcDataReader reader = com.ExecuteReader()) {
      string finalstring = "";
      while (reader.Read()) {
        if(reader.HasRows) {
          finalstring = reader.GetString(9) + ",";
          for (int i = 0; i <= 8; i = i + 1) {
            finalstring = finalstring + reader.GetValue(i).ToString() + ",";
          }
          finalstring = finalstring + "|";
          reader.NextResult();
        }
      }
      if (finalstring != "") {
        finalstring = finalstring.Remove(finalstring.Length -1, 1);
        Response.Write(finalstring);
      }
    }
    noredirect = 1;
  }
}

However, here is the sample output:

00001,0,Pie Johnson,piesaregreat@yum.com,,,10/7/2010 12:00:00 AM,Bakery,N/A,N/A,

As you can see, the second deliminator is not appearing at all, when it really should. Also, this query, when run in heidisql, returns a good number of rows, not just this one result. Once I have it passed to the Javascript, I can figure it out since I have much more experience with that and I've actually done this before via PHP.

I would use a DataTable and a DataAdapter :

String finalString;
var tblPieData = new DataTable();
using(var con = new OdbcConnection(connectionString))
using (OdbcDataAdapter da = new OdbcDataAdapter("SELECT * FROM pie_data WHERE Pie_ID = ?", con))
{
    da.SelectCommand.Parameters.AddWithValue("Pie_ID", reference_id);
    da.Fill(tblPieData);
    var rowFields = tblPieData.AsEnumerable()
                              .Select(r => string.Join(",", r.ItemArray));
    finalString = string.Join("|", rowFields);
}

This line:

finalstring = finalstring.Remove(finalstring.Length -1, 1);

is going to remove the last instance of your pipe-delimiter, so if there is only one record (which there appears to be,) you shouldn't see one.

Unless I am missing something...

EDIT

Actually, if you are looking for two records, you are probably missing the second one because you start your loop with while(reader.Read()) and end it with reader.NextResult(); . These will both advance the reader, causing you to miss every other record.

EDIT 2

You are also overwriting finalstring on each iteration; you probably want to append to this (making it a StringBuilder would make the most sense for efficiency's sake, if you don't know how many records to expect.)

You are replacing the value of finalstring in every iteration of the loop. This will cause only the values for a single row to be returned:

finalstring = reader.GetString(9) + ",";

as well as removing the last character at the end of each iteration through the columns in the rows (the pipe, not the trailing comma as I'm expecting you want):

finalstring = finalstring.Remove(finalstring.Length -1, 1);

EDIT:

It also looks like you are skipping over every other record by both looping on reader.Read() as well as calling reader.NextResult()

Your looping structure with the reader appears to have some problems, not the least of which is the reset of finalAnswer within your loop and giving you only a single result combined with using both .Read() and .NextResult:

Suggested fixes...not tested, so all standard caveats apply :) :

Edited per Mark Averius' comment and OP confirmation of original intent:

using (OdbcDataReader reader = com.ExecuteReader()) {
  if (reader.HasRows())
  {
    string finalstring = "";
    while (reader.Read()) {
      finalstring = finalstring + reader.GetString(9) + ",";
      for (int i = 0; i <= 8; i++) {
        finalstring = finalstring + reader.GetValue(i).ToString() + ",";
      }
      finalstring = finalstring + "|";
    }
    if (finalstring != "") {
      finalstring = finalstring.Remove(finalstring.Length -1, 1);
      Response.Write(finalstring);
    }
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM