如何使用SQL作业自动化代码?

时间:2013-01-29 04:31:15

标签: sql vb.net clr

我有这个VB代码从我的Web服务器删除垃圾文件。代码似乎运行正常,我希望自动化此代码,使用sql job每天运行它。怎么办?

以下是代码:

Public Sub GetFilesAndFolders()
    Dim dirInfo As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/test"))
    Dim fileInfo() As FileInfo = dirInfo.GetFiles("*.*", SearchOption.AllDirectories)
    Dim fileExist As ArrayList = New ArrayList()
    Dim pathExist As ArrayList = New ArrayList()
    Dim fileToDelete As ArrayList = New ArrayList()
    pathExist = returnPathExist()
    For Each d As FileInfo In fileInfo
        If String.Format("{0:dd/MM/yyyy}", d.CreationTime) = Date.Today Then
            fileExist.Add("~\test\" & d.Name)
        End If
    Next

    For Each listElement As String In fileExist
        If Not pathExist.Contains(listElement) Then
            fileToDelete.Add(listElement)
        End If
    Next

    For Each item As String In fileToDelete
        Dim imageFile As FileInfo = New FileInfo(HttpContext.Current.Server.MapPath(item))
        If imageFile.Exists Then
            File.Delete(HttpContext.Current.Server.MapPath(item))
        End If
    Next

End Sub

Function returnPathExist() As ArrayList
    Dim pathExist As ArrayList = New ArrayList()
    'declare connection to the database and stored procedure
    Try
        cnn.Open()
        rdr = cmd.ExecuteReader
        While rdr.Read()
            pathExist.Add(rdr("test"))

        End While
        rdr.Close()
    Finally
        cnn.Close()
        cnn.Dispose()
    End Try
    Return pathExist
End Function

我想使用CLR但不确定我应该添加哪个CLR。模块,类,存储过程或???并且CLR不支持httpcontext和system.Configuration.ConfigurationManager,我需要在上面的returnPathExist函数中访问我的存储过程。

感谢您的建议。

更新

我离这个工作只有一步之遥。以下是引发错误的行:

Dim dirInfo As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/test"))

Dim imageFile As FileInfo = New FileInfo(HttpContext.Current.Server.MapPath(item))

File.Delete(HttpContext.Current.Server.MapPath(item))

似乎HttpContext.Current不能在CLR中使用。这是真的吗?有任何解决方法吗?

感谢。

1 个答案:

答案 0 :(得分:0)

您应该确实拥有路径的配置设置。 HttpContext.Current仅对ASP.NET请求/响应上下文有效。