返回收集的项目 [英] Returning collection of items
问题描述
我正从数据库中检索多条记录并将其存储在列表中。我创建了属性类,然后我将检索到的数据分配给属性中的变量,然后尝试将所有值集合返回给进行函数调用的客户端,但它不起作用。为什么?请正确。
代码:
public 属性FetchCoordinates( String FetchParam)
{
String [] parts = FetchParam.Split(' ,');
sqlCom.CommandText = FetchCoordinates;
sqlCom.CommandType = CommandType.StoredProcedure;
sqlCom.Parameters.Add( @ IMEI,SqlDbType.VarChar ).Value = parts [ 0 ]。ToString();
sqlCom.Parameters.Add( @ DateTimeFrom,SqlDbType.VarChar).Value = Convert.ToDateTime(parts [ 1 ]。ToString());
sqlCom.Parameters.Add( @ DateTimeTo,SqlDbType.VarChar).Value = Convert.ToDateTime(parts [ 2 ]。ToString());
SqlParameter sqlParam = new SqlParameter( @结果,SqlDbType.Int);
sqlCom.Parameters.Add(sqlParam);
sqlCom.Parameters [ @ result]。Direction = ParameterDirection.Output;
列表<属性> props = new List< Properties>();
属性p;
尝试
{
sqlCon.Open();
使用(SqlDataReader reader = sqlCom.ExecuteReader())
{
while (reader.Read())
{
p = new 属性()
{
经度=读者[ 经度]。ToString(),
纬度=读者[ 纬度]。ToString()
};
props.Add(p);
return p;
}
}
}
catch (例外情况)
{
}
最后
{
sqlCon.Close();
}
}
}
Properties.cs < br $> b $ b
public class 属性
{
public 属性()
{
}
< span class =code-keyword> public string 经度{ get ; set ; }
public string 纬度{ get 跨度>; set ; }
}
- 代码在q。返回单个属性值而不是列表/数组/集合
- 该方法在读取数据的第一行后返回
我已经删除了设置代码和异常处理,以便更容易看到更改。
// 如果我们想要所有值的列表,那么请确保该方法返回一个列表。
public List< Properties> FetchCoordinates( String FetchParam)
{
// 为清晰起见而切碎
List< properties> props = new List< properties>();
属性p;
sqlCon.Open();
使用(SqlDataReader reader = sqlCom.ExecuteReader())
{
while (reader.Read())
{
p = new 属性()
{经度=读者[ 经度]。ToString(),
Latitude = reader [ 纬度]。ToString()};
props.Add(p);
// 不要以为你想这样做。
// return p;
}
}
sqlCon.Close() ;
// 返回我们从数据库中读取的_everything_。
返回道具;
}
如果是我的话我还会重命名属性类位置或者可能将坐标重命名为鉴于它的属性,这似乎是一个更好的描述。
我可以立即看到的两件事。
你需要返回一个属性集合而不是属性对象。
public List< properties> FetchCoordinates(String FetchParam)< / properties>
然后你需要删除while循环中的返回值。在循环之后移动你的回报。
返回道具;
I am retrieving multiple records from database and storing it in list. I have created properties class and then i assign data being retrieved to variables in properties and then trying to return all collection of values to client who made function call but it doesn't work. Why ? please make it correct.
Code:
public Properties FetchCoordinates(String FetchParam)
{
String[] parts = FetchParam.Split(',');
sqlCom.CommandText = "FetchCoordinates";
sqlCom.CommandType = CommandType.StoredProcedure;
sqlCom.Parameters.Add("@IMEI", SqlDbType.VarChar).Value = parts[0].ToString();
sqlCom.Parameters.Add("@DateTimeFrom", SqlDbType.VarChar).Value = Convert.ToDateTime(parts[1].ToString());
sqlCom.Parameters.Add("@DateTimeTo", SqlDbType.VarChar).Value = Convert.ToDateTime(parts[2].ToString());
SqlParameter sqlParam = new SqlParameter("@result", SqlDbType.Int);
sqlCom.Parameters.Add(sqlParam);
sqlCom.Parameters["@result"].Direction = ParameterDirection.Output;
List<Properties> props = new List<Properties>();
Properties p;
try
{
sqlCon.Open();
using (SqlDataReader reader = sqlCom.ExecuteReader())
{
while (reader.Read())
{
p = new Properties()
{
Longitude = reader["Longitude"].ToString(),
Latitude = reader["Latitude"].ToString()
};
props.Add(p);
return p;
}
}
}
catch (Exception ex)
{
}
finally
{
sqlCon.Close();
}
}
}
Properties.cs
public class Properties
{
public Properties()
{
}
public string Longitude { get; set; }
public string Latitude { get; set; }
}
- The code in q. returns a single property value not a list/array/collection
- The method returns after the first row in the data has been read
I've stripped the setup code and exception handling to make the changes easier to see.
// If we want a list of all values then make sure the method returns a list. public List<Properties> FetchCoordinates(String FetchParam) { // Chopped for clarity List<properties> props = new List<properties>(); Properties p; sqlCon.Open(); using (SqlDataReader reader = sqlCom.ExecuteReader()) { while (reader.Read()) { p = new Properties() { Longitude = reader["Longitude"].ToString(), Latitude = reader["Latitude"].ToString()}; props.Add(p); // Don't think you want to do this. // return p; } } sqlCon.Close(); // Return _everything_ we read from the DB. return props; }
Were it me I'd also rename the Properties class "Position" or possibly Coordinate as that seems like a better description given its properties.
Two things I can see right away.
You need to return a collection of Properties not the Properties object.
public List<properties> FetchCoordinates(String FetchParam) </properties>
Then you need to remove the return in your while loop. Move your return to after the loop.
return props;
这篇关于返回收集的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!