Home > Software engineering >  SQL DataReader into Arrays
SQL DataReader into Arrays

Time:10-05

//request.Data = new double[] { 221, 428, 492, 471, 413, 344, 294 };  -->Dummy Data    
requestReport.Data =  PurchaseSpProvider.GetRequestReportData();

public static double[] GetRequestReportData()
{            
    using (SqlDataReader dataReader = SqlManager.Instance.ExecuteReader(CommandType.StoredProcedure, "GetRequestReportToDays"))
    { 
        // what here?          
    }
}

I have never tried reading array with sql before. How can I read array type value with datareader?

CodePudding user response:

Use dataReader.Read() in while loop to get data.

//request.Data = new double[] { 221, 428, 492, 471, 413, 344, 294 };  -->Dummy Data    
requestReport.Data =  PurchaseSpProvider.GetRequestReportData();

public static double[] GetRequestReportData()
{
    List<double> list = new List<double>();            
    using (SqlDataReader dataReader = SqlManager.Instance.ExecuteReader(CommandType.StoredProcedure, "GetRequestReportToDays"))
    { 

        while (dataReader.Read()) {
          list.Add(dataReader.GetDouble(0));   // 0: is your index, it can be 0, 1, 2,... based on your column index
        }
    }

    return list.ToArray();
}
  • Related