I have a query that selects rows according to a certain defined criteria. Once that happens I have a reader that gets the values for use. My problem is how can I get a variable amount of rows the reader returned to my ASP.Net behind code page? Also be able to get the individual rows and not just the first row. I am not sure if a counter needs to be set or honestly what to do. How can I get these variable amounts of rows into the appropriate labels? If you want to see any code let me know.
DBHelper class where:
private DBHelper() { }
public static SqlConnection getConnection()
{
return new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
}
public static SqlConnection getFRESHConnection()
{
return new SqlConnection(ConfigurationManager.ConnectionStrings["FRESHConnection"].ConnectionString);
}
public static SqlDataReader executeQuery(SqlConnection dbConn, string sqlString, SqlParameter[] parameters)
{
SqlCommand cmd = null;
SqlDataReader reader = null;
try
{
if (dbConn.State == ConnectionState.Closed)
dbConn.Open();
cmd = dbConn.CreateCommand();
cmd.CommandText = sqlString;
if (parameters != null)
{
cmd.Parameters.AddRange(parameters);
}
reader = cmd.ExecuteReader();
cmd.Dispose();
}
catch (Exception ex)
{
throw ex;
}
return reader;
}
DataClass:
public class LineAData
{
public int CaseNum6;
public int CaseNum9;
public int Group;
public bool Completed;
public int WorkID;
public void load(int Group1, int Group2)
{
StringBuilder sqlString = new StringBuilder();
sqlString.Append("SELECT WorkID, CaseNum6, CaseNum9, Group, Completed ");
sqlString.Append("FROM WorkOrder ");
sqlString.Append("WHERE Group = @Group1 OR Group = @Group2 ");
sqlString.Append("AND Completed = '0' ");
SqlDataReader reader = null;
SqlConnection dbConn = DBHelper.getConnection();
SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Group1", Group1 ),
new SqlParameter("@Group2", Group2) };
try
{
reader = DBHelper.executeQuery(dbConn, sqlString.ToString(), parameters);
if (reader != null)
{
if (reader.Read())
{
WorkID = (int)(reader["WorkID"]);
CaseNum6 = (int)(reader["CaseNum6"]);
CaseNum9 = (int)(reader["CaseNum9"]);
Group = (int)(reader["Group"]);
Completed = (bool)reader["Completed"];
}
else
throw new Exception("No record returned");
reader.Close();
reader.Dispose();
dbConn.Close();
dbConn.Dispose();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (dbConn != null)
{
try { dbConn.Close(); dbConn.Dispose(); }
catch { }
}
if (reader != null)
{
try { reader.Close(); reader.Dispose(); }
catch { }
}
}
}
Create a class that stores the following fields:
public class Class1
{
public int WorkId { get; set; }
public int CaseNum6 { get; set; }
public int CaseNum9 { get; set; }
public int Group { get; set; }
public bool Completed { get; set; }
}
Then instead of using if (reader.Read())
Use while(reader.read())
List<Class1> myList = new List<Class1>();
while(reader.read())
{
// assign the reader values to your class
myClass obj = new myClass();
obj.WorkId= reader["WorkId"]
obj.CaseNum6 = reader["CaseNum6"]
// do for all fields
// add to the list
myList.Add(obj)
}
Now you have a variable number of results stored in "myList"!
Of course, remember to close your reader or wrap it in a using statement.
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.