[英]Inserting multiple rows into html table ASP.Net
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?
我的问题是如何使读者返回到代码页背后的ASP.Net的行数可变? 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: 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;
}
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()) 然后代替使用
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)
}
Now you have a variable number of results stored in "myList"! 现在,“ myList”中存储了可变数量的结果!
Of course, remember to close your reader or wrap it in a using statement. 当然,请记住关闭阅读器或将其包装在using语句中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.