运行以下代码时,它会遗漏一行。当我执行 files.Count 时,它说有 4 行,但没有为第 4 行存储数据。当我从 SQL 管理器中运行存储过程时,它会返回所有 4 行和所有数据。有什么帮助吗?
List<File> files = new List<File>();
SqlConnection active_connection = new SqlConnection(m_connection_string);
SqlCommand cmd = new SqlCommand();
SqlDataReader dr = null;
try
{
active_connection.Open();
cmd.Connection = active_connection;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "dalsp_Select_Organization_Files";
SqlParameter param;
param = cmd.Parameters.Add("@p_organization_guid", SqlDbType.UniqueIdentifier);
param.Value = new Guid(organization_guid);
param = cmd.Parameters.Add("@p_file_type", SqlDbType.NVarChar, 50);
param.Value = file_type;
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.HasRows)
{
while (dr.Read())
{
File file = new File();
file.OrganizationGuid = dr["OrganizationGuid"].ToString();
file.FileGuid = dr["FileGuid"].ToString();
file.FileLocation = dr["FileLocation"].ToString();
file.FileName = dr["FileName"].ToString();
file.FileType = (FileTypeEnum)Enum.Parse(typeof(FileTypeEnum), dr["FileType"].ToString());
file.FileExtension = dr["FileExtension"].ToString();
file.FileDescription = dr["FileDescription"].ToString();
file.ThumbnailPath = dr["ThumbnailPath"].ToString();
files.Add(file);
}
}
dr.Close();
dr = null;
active_connection.Close();
cmd = null;
}
catch (Exception)
{
throw;
}
finally
{
if (active_connection.State != ConnectionState.Closed)
{
active_connection.Close();
active_connection.Dispose();
}
}
return files;