運行以下代碼時,它會留下一行。當我做一個files.Count它說有4行,但沒有數據存儲的第四行。當我從SQL管理器中運行存儲過程時,它將返回所有4行和所有數據。任何幫助?SQL DataReader在循環中缺少一行
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;
謝謝馬克,因爲代碼我不用手寫這個了。 – 2008-11-20 08:59:15
+1引起這似乎最有可能對我來說 – 2008-11-20 09:18:52