2013-07-09 48 views
0
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("App_Data\\people.mdb")); 
      string command = "SELECT * from Files where FileSubjectID=1"; //1 = newsLetters, 2 = Publications, 3 = Videos, 4 = Presentations, 5 = Brochures, 6 = Others 
      OleDbDataReader reader = null;  
      string fileType = string.Empty; 
      string fileIcon = string.Empty; 
      FileInfo file; 

      int count = 0; 

      try 
      { 
       using (conn) 
       { 
        using (OleDbCommand cmd = new OleDbCommand(command, conn)) 
        { 
         cmd.CommandType = CommandType.Text; 
         conn.Open(); 
         reader = cmd.ExecuteReader(); 

         Response.Write("bofore whil1e<br/>" + reader.HasRows + "<br/>"); 
         while (reader.Read()) 
         { 

          file = new FileInfo(reader["FileName"].ToString()); 
          fileIcon = setFileTypeIcon(file.Extension.ToLower()); 

          Response.Write(count++ + "<br/>"); 


          lblNewsLetters.InnerHtml += "<tr><td class='te_al_M'>" + 
           "<table><tr><td rowspan='2'><img src='images/icons/" + fileIcon + ".png' alt='" + fileIcon + "'/></td><td><span class='te_al_L lblFiles'>" + reader["FileName"].ToString() + "</span></td></td></tr><tr><td><input type='text' style='display:none' id='txtNewName_" + reader["FileID"].ToString() + "'/></td></tr></table></td>" + 


           "<td class='te_al_L' width='150'>" + getUserName(reader["UserID"].ToString()) + "</a></td>" + 
           "<td class='te_al_L' width='70'>" + Convert.ToDateTime(reader["FileUploadDate"].ToString()).ToShortDateString() + "</td>" + 
           "<td class='te_al_L' width='70'><a target='_blank' href='" + reader["FilePath"].ToString() + "''><img src='images/viewBtn.jpg' alt='View file' title='View file'/></a></td>" + 
           "<td class='te_al_L' width='70'><a target='_blank' href='" + reader["FilePath"].ToString() + "''><img src='images/downloadBtn.jpg' alt='Download file' title='Download file'/></a></td>"; 


          // only the uploader can change the file name 
          if (isCurrentUsercodeInFile(currentUsercode, reader["FileName"].ToString())) 
          { 
           lblNewsLetters.InnerHtml += "<td class='width_70'>" + 
            "<a id='rename_" + reader["FileID"].ToString() + "' href='javascript:renameFile(\"" + reader["FileID"].ToString() + "\");'><img src='images/renameBtn.jpg' alt='Rename file' title='Rename file'/></a>" + 
            "<a style='display:none;' id='save_" + reader["FileID"].ToString() + "' href='javascript:saveFile(\"" + reader["FilePath"].ToString() + "\" , \"" + reader["FileName"].ToString() + "\" , \"" + currentUsercode + "\" , \"" + reader["FileID"].ToString() + "\");'><img src='images/saveBtn.jpg' alt='Save file' title='Save file'/></a></td>"; 
          } 
          else 
          { 
           lblNewsLetters.InnerHtml += "<td class='width_70'>&nbsp;</td>"; 
          } 


          // only the uploader can delete the file 
          if (isCurrentUsercodeInFile(currentUsercode, reader["FileName"].ToString())) 
          { 
           lblNewsLetters.InnerHtml += "<td class='width_70'><a href='javascript:delFile(\"" + reader["FilePath"].ToString() + "\" , \"" + reader["FileName"].ToString() + "\" , \"" + currentUsercode + "\");'><img src='images/deleteBtn.jpg' alt='Delete file' title='Delete file'/></a></td></tr>"; 
          } 
          else 
          { 
           lblNewsLetters.InnerHtml += "<td class='width_70'>&nbsp;</td></tr>"; 
          } 
         } 
         Response.Write("b whil1e<br/>"); 
         conn.Close(); 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       //Response.Write(ex.Message); 
       conn.Close(); 
      } 

我有上面的代码,忽略所有的“Response.Write”和拼写,他们只是为我的测试。从访问中获取数据while while

我的问题是,要在本地编码工作,但不能联机。 “while(reader.Read())”不能在线执行,我看到的是我之前的消息,而不是while循环中的消息,而不是while循环结束时的消息,而在本地我看到所有消息。

我做错了什么?我在这工作了两天。如果你需要更多的代码,请告诉需要什么,我很抱歉,如果这是问,但我没有找到解决我的问题。

+0

你可以检查OLEDB驱动程序吗? – Amit

+0

@AKA,你是什么意思来检查OLEDB驱动程序? 如何做到这一点? –

+0

尝试安装oledb驱动程序。那么它会显示,这台机器havinf OLEDB或不 – Amit

回答

2

你吞咽异常:

catch (Exception ex) 
{ 
    //Response.Write(ex.Message); 
    conn.Close(); 
} 

你是不是while循环后,看不到任何理由开始可能是其被抛出,但随后吞噬在片段上方例外。我会记录异常并分析它。它可能是沿线,你无法找到数据库中指定的文件:

file = new FileInfo(reader["FileName"].ToString());` 

因此,因此引发和失败。 从不燕子例外!至少登录到一个文件,以便确定发生了什么。

+0

谢谢,这是我的愚蠢。 我得到这个错误: “请求类型'System.Security.Permissions.FileIOPermission,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的权限失败。” 网站存储在godaddy if这意味着什么 –

+0

这通常意味着您的应用程序没有足够的权限。去爸爸可能是限制和完成一个托管案件的基础上。但是,您可以尝试将此添加到'System.web'下的'web.config':' />'这应该提升您的应用程序的完全信任度。 – Belogix

+0

您是否设法解决Go Daddy问题?如果这有帮助,请标记为已回答,以帮助其他有类似问题的人。谢谢。 – Belogix