我有一个查询,根据特定的标准选择行。一旦发生这种情况,我就有了一个能够获得使用价值的读者。我的问题是如何获得读者返回到代码页后面的ASP.Net的可变行数?也能够获得单独的行而不仅仅是第一行。我不确定是否需要设置柜台或诚实地做什么。如何将这些可变数量的行放入适当的标签中?如果您想查看任何代码,请告诉我。
DBHelper类其中:
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;
}
数据类:
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 { }
}
}
}
答案 0 :(得分:0)
创建一个存储以下字段的类:
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; }
}
然后而不是使用if (reader.Read())
使用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)
}
现在,您在“myList”中存储了可变数量的结果!
当然,请记得关闭阅读器或将其包装在使用声明中。