用while获取访问数据

时间:2013-07-09 08:18:08

标签: c# asp.net database ms-access while-loop

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(reader.Read())”没有在线执行我看到的是我之前的消息,而不是while循环中的消息,而不是while循环结束时的消息,而在本地我看到所有消息

我做错了什么?我在这工作了两天。如果您需要更多代码,请告诉我们需要什么,对不起,如果有人问我,但我找不到解决问题的方法。

1 个答案:

答案 0 :(得分:2)

你吞下这个例外:

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

while循环开始后你没有看到任何东西的原因可能是抛出的异常,但随后在上面的代码段中被吞噬。我会记录异常并分析它。您可能无法找到数据库中指定的文件:

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

所以因此抛出并失败。 从不吞下异常!至少登录到文件,以便确定发生了什么。