2008-11-20 75 views
2

运行以下代码时,它会留下一行。当我做一个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; 

回答

5

如果你说你的文件集合有4个项目,但4个项目不包含任何值,那么你的意思是什么?它是否为空,对象是否没有数据,或者是否将索引超出范围异常?

你正在做一个文件[4]或类似以下的东西?

for(int x = 1; x < files.length; x++) 
{ 
    files[x] 
} 

这是行不通的。在C#中记住基于0的索引。

作为一个侧面说明,你可以通过执行类似弄死你尝试捕捉statments:

using (SqlConnection connection = new SqlConnection(conn_string)) 
{ 
    connection.Open(); 
    using (SqlCommand cmd = new SqlCommand("SELECT * FROM MyTable", connection)) 
    { 
     using (SqlDataReader dr = cmd.ExecuteReader()) 
     { 
      return result; 
     } 
    } 
} 

using语句将保证读写器和连接处理(并因此关闭)。

+0

谢谢马克,因为代码我不用手写这个了。 – 2008-11-20 08:59:15

+0

+1引起这似乎最有可能对我来说 – 2008-11-20 09:18:52

1

您是否尝试在调试器中逐步完成此操作,并在执行读取器之前检查您的命令参数?你是否在结果集中获得与在sql上直接运行sproc直接相同的值?

我可能是错的,因为有几种方法可以做到这一点,但是在将命令添加到命令中的方式看起来有些扭曲。

我通常使用的模式更像是:

SqlParameter param = new SqlParameter(); 
// set param stuff - here or in ctor 
cmd.Parameters.Add(param); 

主要是关于你的作品....

大多数,我曾与存储过程的问题时代的是,当我pooched的参数。

1

该代码看起来对我来说是正确的。我想你肯定会想通过一个调试器来查看从ExecuteReader返回的行数。我的一个意见是,“if(dr.HasRows)”有点多余,因为“while(dr.Read())”会给你同样的效果。

我会有的另一个问题是,你知道如果你错过了第一个或最后一个记录?

布赖恩

2

你应该抛弃“如果(dr.HasRows)”,它的作用是复制在while循环检查。

您不应该在连接上调用Close,也不应将其设置为null。相反,您应该将连接包装在“使用”块中,如下所示:

using (SqlCommand cmd = new SqlCommand()) { 
    //use the connection 
} 

对于您的数据读取器和SQL命令也是如此。

这下一个位什么也不做,删除它。

catch (Exception) { throw; }