简体   繁体   中英

ODBC SQL Server Stored Procedure Not Returning All Rows

I am trying to use ExecuteReader on my stored procedure to get a list of rates. this is the output I am expecting which is from SSMS. I am only getting one row from the reader instead of the 35 I am supposed to be getting.

速度

The code I am using to try and achieve this is

OdbcCommand ratesCmd = new OdbcCommand("EXEC SP_LABOR_MODELS ?,?", rfqCon);
ratesCmd.CommandType = CommandType.StoredProcedure;

OdbcParameter initParam = new OdbcParameter("@init", 1);
OdbcParameter calcrefParam = new OdbcParameter("@calcrefid", DBNull.Value);

rfqCon.Open();

ratesCmd.Parameters.Add(initParam);
ratesCmd.Parameters.Add(calcrefParam);

OdbcDataReader ratesReader = ratesCmd.ExecuteReader();
while(ratesReader.Read()) {
   if(reader[0] != DBNull.Value){
   }
   if(reader[1] != DBNull.Value){
      //Fails here & says index was outside the bounds of the array.
      //There should be 35 rows that get returned to the reader.
   }
}

I have tried many things like trying to change the way I am calling the execute on the stored procedure like so.

= new OdbcCommand("{call SP_LABOR_MODELS(?,?)}", rfqCon);

The issue you are having is that the records being returned only have a single field, but reader[1] is trying to access a second field. The reader exposes a single record at a time, and the index refers to the column/field within the record. In the while loop, call to ratesReader.Read() will load the next record, and when the end of the record set is reached, the Read() method will return false and exit the loop.

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