正在返回项目列表
本文关键字:列表 项目 返回 | 更新日期: 2023-09-27 17:58:23
我正在从数据库中检索多条记录并将其存储在列表中。我已经创建了属性类,然后我将要检索的数据分配给属性中的变量,然后尝试将所有值的集合返回给进行函数调用的客户端,但它不起作用。为什么?请改正。
代码:
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();
}
}
}
属性.cs
public class Properties
{
public Properties()
{
}
public string Longitude { get; set; }
public string Latitude { get; set; }
}
更改方法签名;
public List<Properties> FetchCoordinates(String FetchParam)
而不是返回仅为单个实例的p
,而是返回List props;
while (reader.Read())
{
p = new Properties()
{
Longitude = reader["Longitude"].ToString(),
Latitude = reader["Latitude"].ToString()
};
props.Add(p);
}
return props;