一个接一个地删除多个文件

时间:2014-09-21 09:34:47

标签: c#

我试图以这种方式从目录中一个接一个地删除两个文件:

 protected void BtnDelete_Click(object sender, EventArgs e)
{
    string pdfUrl = string.Empty;
    string imgUrl = string.Empty;
    SqlCommand sCmd = new SqlCommand(string.Format("SELECT PDFUrl, ImgUrl FROM Book WHERE Id='{0}'", TextBoxDelete.Text.Trim()), con);
    con.Open();
    Object result = sCmd.ExecuteScalar();
    con.Close();
    if (result != DBNull.Value)
    {
        pdfUrl = result.ToString();
        imgUrl = result.ToString();

        string fullPathPDF = Server.MapPath(pdfUrl);
        string fullPathImg = Server.MapPath(imgUrl);

        if (File.Exists(fullPathPDF))
        {
            File.Delete(fullPathPDF);
        }

        if (File.Exists(fullPathImg))
        {
            File.Delete(fullPathImg);
        }
    }

    SqlCommand cmd = new SqlCommand("DELETE FROM Book WHERE Id='" + TextBoxDelete.Text.ToString() + "'", con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
}

但只有第一个文件被删除。如何删除这两个文件 - 一个接一个,因为在这种情况下只会删除PDFUrl

1 个答案:

答案 0 :(得分:2)

SqlCommand.ExecuteScalar无法返回两个结果 它返回第一行的第一列,因此您的imgUrl字符串与pdfUrl字符串相同。

您应该更改代码以使用SqlDataReader

protected void BtnDelete_Click(object sender, EventArgs e)
{
    string pdfUrl = string.Empty;
    string imgUrl = string.Empty;

    // Using a parameterized query to avoid malicious user text that could wreak havoc....
    // Sql Injection is a serious problem. Always use a parameterized query......
    string cmdText = @"SELECT PDFUrl, ImgUrl FROM Book WHERE Id=@id";

    // Enclose disposable objects in a using statement 
    // DO NOT KEEP a global connection object... there is the connection pool
    using(SqlConnection con = new SqlConnection(.....connectionstringhere....))
    using(SqlCommand cmd = new SqlCommand(cmdText, con))
    {
        con.Open();
        cmd.Parameters.AddWithValue("@id", TextBoxDelete.Text.Trim());

        // ask the command to create a reader for us....
        using(SqlDataReader reader = cmd.ExecuteReader())
        {
            // Check if a record is returned by the reader
            if(reader.Read())
            {
               // OK we could get it but not if it is DBNull.....
               pdfUrl = reader.IsDbNull(0) ? string.Empty : reader[0].ToString();
               imgUrl = reader.IsDbNull(1) ? string.Empty : reader[1].ToString();

               // Prepare the full path for the two files...
               string fullPathPDF = pdfUrl.Length > 0 ? Server.MapPath(pdfUrl) : string.Empty;
               string fullPathImg = imgUrl.Length > 0 ? Server.MapPath(imgUrl) : string.Empty;

               // Start deleting them
               if (File.Exists(fullPathPDF))
                   File.Delete(fullPathPDF);
               if (File.Exists(fullPathImg))
                   File.Delete(fullPathImg);

               // CLOSE THE READER BEFORE EXECUTING ANOTHER COMMAND
               // This could be removed if your connection string uses 
               // MultipleActiveResultSets=True   (MARS)
               reader.Close();

               // The parameter is still there, the command is still linked to the connection
               // Just change the commandtext and execute....
               cmd.CommandText = "DELETE FROM Book WHERE Id=@id";
               cmd.ExecuteNonQuery();
            }
        }
    }
}