C# 在c中从两个方法添加列表#
我正在尝试将getRecords列表2添加到getRecords列表中。由于某些原因,在getRecords上,处理它需要很长时间,当我调用它时会超时C# 在c中从两个方法添加列表#,c#,list,add,C#,List,Add,我正在尝试将getRecords列表2添加到getRecords列表中。由于某些原因,在getRecords上,处理它需要很长时间,当我调用它时会超时 public static List<ListA> getRecords2(string id) { List<ListA> listOfRecords = new List<ListA>(); using (SqlConnection con = SqlConnect.GetDBConnectio
public static List<ListA> getRecords2(string id)
{
List<ListA> listOfRecords = new List<ListA>();
using (SqlConnection con = SqlConnect.GetDBConnection())
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "sp2";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@id", id));
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
ListA listMember = new ListA();
listMember.ID = (int)reader["ID"];
listMember.Name = reader["FullName"].ToString().Trim();
}
con.Close();
}
return listOfRecords;
}
public static List<ListA> getRecords(string id)
{
List<ListA> listOfRecords = new List<ListA>();
using (SqlConnection con = SqlConnect.GetDBConnection())
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "sp1";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@id", id));
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
ListA listMember = new ListA();
listMember.ID = (int)reader["ID"];
listMember.Name = reader["FullName"].ToString().Trim();
}
con.Close();
}
List<ListA> newlist = getRecords(id);
foreach (ListA x in newlist) listOfRecords.Add(x);
return listOfRecords;
}
公共静态列表getRecords2(字符串id)
{
List listOfRecords=新列表();
使用(SqlConnection con=SqlConnect.GetDBConnection())
{
con.Open();
SqlCommand cmd=con.CreateCommand();
cmd.CommandText=“sp2”;
cmd.CommandType=CommandType.storedProcess;
cmd.Parameters.Add(新的SqlParameter(“@id”,id));
SqlDataReader=cmd.ExecuteReader();
while(reader.Read())
{
ListA listMember=新ListA();
listMember.ID=(int)读卡器[“ID”];
listMember.Name=reader[“FullName”].ToString().Trim();
}
con.Close();
}
返回记录列表;
}
公共静态列表getRecords(字符串id)
{
List listOfRecords=新列表();
使用(SqlConnection con=SqlConnect.GetDBConnection())
{
con.Open();
SqlCommand cmd=con.CreateCommand();
cmd.CommandText=“sp1”;
cmd.CommandType=CommandType.storedProcess;
cmd.Parameters.Add(新的SqlParameter(“@id”,id));
SqlDataReader=cmd.ExecuteReader();
while(reader.Read())
{
ListA listMember=新ListA();
listMember.ID=(int)读卡器[“ID”];
listMember.Name=reader[“FullName”].ToString().Trim();
}
con.Close();
}
List newlist=getRecords(id);
foreach(新列表中的listax)listOfRecords.Add(x);
返回记录列表;
}
我在getRecords2中添加了getRecords列表。我做错了吗?首先,您没有在
while(reader.Read())
循环中向列表添加任何内容<在GetRecords
和GetRecords2
上都缺少code>Add方法调用:
while (reader.Read())
{
ListA listMember = new ListA();
listMember.ID = (int)reader["ID"];
listMember.Name = reader["FullName"].ToString().Trim();
// you have to add this line:
listOfRecords.Add(listMember);
}
还有另一个问题:您一遍又一遍地调用getRecords(id)
:
List<ListA> newlist = getRecords(id);
foreach (ListA x in newlist) listOfRecords.Add(x);
List newlist=getRecords(id);
foreach(新列表中的listax)listOfRecords.Add(x);
应该是:
List<ListA> newlist = getRecords2(id);
foreach (ListA x in newlist) listOfRecords.Add(x);
List newlist=getRecords2(id);
foreach(新列表中的listax)listOfRecords.Add(x);
最后但并非最不重要的一点是:您不必调用
con.Close()代码>-当程序流退出时,作为Dispose
方法的一部分,它将使用块自动完成。您在这里有递归调用List newlist=getRecords(id)代码>看起来getRecords在自我调用?@JakeP和lazyberezovsky。。。非常感谢你们,伙计们…只需要多看几双眼睛来解决我的问题。。。英雄联盟我想我需要休息一下如果我是你,我会打开,看看代码的哪个部分占用的时间最多